mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
Merge branch '0.14.0'
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* Copyright 2015 IBM Corp.
|
||||
* Copyright 2015,2016 IBM Corp.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -41,193 +41,284 @@ describe('exec node', function() {
|
||||
n1.should.have.property("cmd", "");
|
||||
n1.should.have.property("append", "");
|
||||
n1.should.have.property("addpay",true);
|
||||
n1.should.have.property("timer",0);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
describe('calling exec', function() {
|
||||
describe('calling exec', function() {
|
||||
|
||||
it('should exec a simple command', function(done) {
|
||||
var flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"echo", addpay:false, append:""},
|
||||
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
||||
var spy = sinon.stub(child_process, 'exec',
|
||||
function(arg1,arg2,arg3,arg4){
|
||||
//console.log(arg1);
|
||||
// arg3(error,stdout,stderr);
|
||||
arg3(null,arg1,arg1.toUpperCase());
|
||||
});
|
||||
it('should exec a simple command', function(done) {
|
||||
var flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"echo", addpay:false, append:""},
|
||||
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
||||
var spy = sinon.stub(child_process, 'exec',
|
||||
function(arg1,arg2,arg3,arg4) {
|
||||
//console.log(arg1);
|
||||
// arg3(error,stdout,stderr);
|
||||
arg3(null,arg1,arg1.toUpperCase());
|
||||
});
|
||||
|
||||
helper.load(execNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
var n3 = helper.getNode("n3");
|
||||
var n4 = helper.getNode("n4");
|
||||
n2.on("input", function(msg) {
|
||||
//console.log(msg);
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.should.be.a.String.and.equal("echo");
|
||||
});
|
||||
n3.on("input", function(msg) {
|
||||
//console.log(msg);
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.should.be.a.String.and.equal("ECHO");
|
||||
done();
|
||||
child_process.exec.restore();
|
||||
});
|
||||
n1.receive({payload:"and"});
|
||||
});
|
||||
});
|
||||
helper.load(execNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
var n3 = helper.getNode("n3");
|
||||
var n4 = helper.getNode("n4");
|
||||
n2.on("input", function(msg) {
|
||||
//console.log(msg);
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.should.be.a.String;
|
||||
msg.payload.should.equal("echo");
|
||||
});
|
||||
n3.on("input", function(msg) {
|
||||
//console.log(msg);
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.should.be.a.String,
|
||||
msg.payload.should.equal("ECHO");
|
||||
done();
|
||||
child_process.exec.restore();
|
||||
});
|
||||
n1.receive({payload:"and"});
|
||||
});
|
||||
});
|
||||
|
||||
it('should exec a simple command with extra parameters', function(done) {
|
||||
var flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"echo", addpay:true, append:"more"},
|
||||
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
||||
var spy = sinon.stub(child_process, 'exec',
|
||||
function(arg1,arg2,arg3,arg4){
|
||||
//console.log(arg1);
|
||||
// arg3(error,stdout,stderr);
|
||||
arg3(null,arg1,arg1.toUpperCase());
|
||||
});
|
||||
it('should exec a simple command with extra parameters', function(done) {
|
||||
var flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"echo", addpay:true, append:"more"},
|
||||
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
||||
var spy = sinon.stub(child_process, 'exec',
|
||||
function(arg1,arg2,arg3,arg4) {
|
||||
//console.log(arg1);
|
||||
// arg3(error,stdout,stderr);
|
||||
arg3(null,arg1,arg1.toUpperCase());
|
||||
});
|
||||
|
||||
helper.load(execNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
var n3 = helper.getNode("n3");
|
||||
var n4 = helper.getNode("n4");
|
||||
n2.on("input", function(msg) {
|
||||
//console.log(msg);
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.should.be.a.String.and.equal("echo and more");
|
||||
});
|
||||
n3.on("input", function(msg) {
|
||||
//console.log(msg);
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.should.be.a.String.and.equal("ECHO AND MORE");
|
||||
done();
|
||||
child_process.exec.restore();
|
||||
});
|
||||
n1.receive({payload:"and"});
|
||||
});
|
||||
});
|
||||
helper.load(execNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
var n3 = helper.getNode("n3");
|
||||
var n4 = helper.getNode("n4");
|
||||
n2.on("input", function(msg) {
|
||||
//console.log(msg);
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.should.be.a.String;
|
||||
msg.payload.should.equal("echo and more");
|
||||
});
|
||||
n3.on("input", function(msg) {
|
||||
//console.log(msg);
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.should.be.a.String;
|
||||
msg.payload.should.equal("ECHO AND MORE");
|
||||
done();
|
||||
child_process.exec.restore();
|
||||
});
|
||||
n1.receive({payload:"and"});
|
||||
});
|
||||
});
|
||||
|
||||
it('should be able to return a binary buffer', function(done) {
|
||||
var flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"echo", addpay:true, append:"more"},
|
||||
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
||||
var spy = sinon.stub(child_process, 'exec',
|
||||
function(arg1,arg2,arg3,arg4){
|
||||
//console.log(arg1);
|
||||
// arg3(error,stdout,stderr);
|
||||
arg3("error",new Buffer([0x01,0x02,0x03]),"");
|
||||
});
|
||||
it('should be able to return a binary buffer', function(done) {
|
||||
var flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"echo", addpay:true, append:"more"},
|
||||
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
||||
var spy = sinon.stub(child_process, 'exec',
|
||||
function(arg1,arg2,arg3,arg4) {
|
||||
//console.log(arg1);
|
||||
// arg3(error,stdout,stderr);
|
||||
arg3("error",new Buffer([0x01,0x02,0x03,0x88]));
|
||||
});
|
||||
helper.load(execNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
var n3 = helper.getNode("n3");
|
||||
var n4 = helper.getNode("n4");
|
||||
n2.on("input", function(msg) {
|
||||
//console.log("n2",msg);
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.should.be.a.Buffer;
|
||||
msg.payload.length.should.equal(4);
|
||||
done();
|
||||
child_process.exec.restore();
|
||||
});
|
||||
n1.receive({});
|
||||
});
|
||||
});
|
||||
|
||||
helper.load(execNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
var n3 = helper.getNode("n3");
|
||||
var n4 = helper.getNode("n4");
|
||||
n2.on("input", function(msg) {
|
||||
//console.log(msg);
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.should.be.a.Buffer;
|
||||
msg.payload.length.should.equal(3);
|
||||
});
|
||||
n4.on("input", function(msg) {
|
||||
//console.log(msg);
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.should.be.a.String.and.equal("error");
|
||||
done();
|
||||
child_process.exec.restore();
|
||||
});
|
||||
n1.receive({payload:"and"});
|
||||
});
|
||||
});
|
||||
});
|
||||
it('should be able to timeout a long running command', function(done) {
|
||||
var flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"sleep", addpay:false, append:"1", timer:"0.3"},
|
||||
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
||||
|
||||
describe.skip('calling spawn', function() {
|
||||
helper.load(execNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
var n3 = helper.getNode("n3");
|
||||
var n4 = helper.getNode("n4");
|
||||
n4.on("input", function(msg) {
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.should.have.property("killed",true);
|
||||
//done();
|
||||
});
|
||||
setTimeout(function() {
|
||||
var logEvents = helper.log().args.filter(function(evt) {
|
||||
return evt[0].type == "exec";
|
||||
});
|
||||
var i = logEvents.length - 1;
|
||||
//logEvents.should.have.length(1);
|
||||
logEvents[i][0].should.have.a.property('msg');
|
||||
logEvents[i][0].msg.toString().should.startWith("Exec node timeout");
|
||||
done();
|
||||
},400);
|
||||
n1.receive({});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should spawn a simple command', function(done) {
|
||||
var flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"echo", addpay:true
|
||||
, append:"", useSpawn:true},
|
||||
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
||||
var events = require('events');
|
||||
//var spy = sinon.stub(child_process, 'spawn',
|
||||
//function(arg1,arg2) {
|
||||
//console.log(arg1,arg2);
|
||||
//var blob = new events.EventEmitter;
|
||||
describe('calling spawn', function() {
|
||||
|
||||
//blob.stdout = function() { blob.emit("data","A"); }
|
||||
//blob.stderr = function() { blob.emit("data","B"); }
|
||||
it('should spawn a simple command', function(done) {
|
||||
var flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"echo", addpay:true, append:"", useSpawn:true},
|
||||
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
||||
var events = require('events');
|
||||
|
||||
//console.log("blob",blob);
|
||||
//setTimeout( function() {
|
||||
//blob.emit("close","CLOSE");
|
||||
//},150);
|
||||
helper.load(execNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
var n3 = helper.getNode("n3");
|
||||
var n4 = helper.getNode("n4");
|
||||
n2.on("input", function(msg) {
|
||||
//console.log(msg);
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.should.be.a.String;
|
||||
msg.payload.should.equal("hello world\n");
|
||||
done();
|
||||
});
|
||||
n1.receive({payload:"hello world"});
|
||||
});
|
||||
});
|
||||
|
||||
//return blob;
|
||||
//});
|
||||
it('should spawn a simple command with a non string payload parameter', function(done) {
|
||||
var flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"echo", addpay:true, append:" deg C", useSpawn:true},
|
||||
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
||||
|
||||
helper.load(execNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
var n3 = helper.getNode("n3");
|
||||
var n4 = helper.getNode("n4");
|
||||
n2.on("input", function(msg) {
|
||||
//console.log(msg);
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.should.be.a.String.and.equal("hello world\n");
|
||||
done();
|
||||
//child_process.spawn.restore();
|
||||
});
|
||||
n1.receive({payload:"hello world"});
|
||||
});
|
||||
});
|
||||
helper.load(execNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
var n3 = helper.getNode("n3");
|
||||
var n4 = helper.getNode("n4");
|
||||
n2.on("input", function(msg) {
|
||||
//console.log(msg);
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.should.be.a.String;
|
||||
msg.payload.should.equal("12345 deg C\n");
|
||||
done();
|
||||
});
|
||||
n1.receive({payload:12345});
|
||||
});
|
||||
});
|
||||
|
||||
it('should spawn a simple command with a non string payload parameter', function(done) {
|
||||
var flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"echo", addpay:true, append:" deg C", useSpawn:true},
|
||||
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
||||
//var spy = sinon.stub(child_process, 'spawn',
|
||||
//function(arg1,arg2) {
|
||||
//console.log(arg1,arg2);
|
||||
////console.log(this);
|
||||
//// arg3(error,stdout,stderr);
|
||||
////arg3(null,arg1,arg1.toUpperCase());
|
||||
//});
|
||||
it('should spawn a simple command and return binary buffer', function(done) {
|
||||
var flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"echo", addpay:true, append:"", useSpawn:true},
|
||||
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
||||
|
||||
helper.load(execNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
var n3 = helper.getNode("n3");
|
||||
var n4 = helper.getNode("n4");
|
||||
n2.on("input", function(msg) {
|
||||
//console.log(msg);
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.should.be.a.String.and.equal("12345 deg C\n");
|
||||
done();
|
||||
});
|
||||
n1.receive({payload:12345});
|
||||
});
|
||||
});
|
||||
helper.load(execNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
var n3 = helper.getNode("n3");
|
||||
var n4 = helper.getNode("n4");
|
||||
n2.on("input", function(msg) {
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.should.be.a.Buffer;
|
||||
msg.payload.length.should.equal(7);
|
||||
done();
|
||||
});
|
||||
n1.receive({payload:new Buffer([0x01,0x02,0x03,0x88])});
|
||||
});
|
||||
});
|
||||
|
||||
it('should error if passed multiple words to spawn command', function(done) {
|
||||
var flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"echo this wont work", addpay:false, append:"", useSpawn:true},
|
||||
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
||||
helper.load(execNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
var n3 = helper.getNode("n3");
|
||||
var n4 = helper.getNode("n4");
|
||||
setTimeout(function() {
|
||||
var logEvents = helper.log().args.filter(function(evt) {
|
||||
return evt[0].type == "exec";
|
||||
});
|
||||
//console.log(logEvents);
|
||||
logEvents.should.have.length(1);
|
||||
logEvents[0][0].should.have.a.property('msg');
|
||||
logEvents[0][0].msg.toString().should.startWith("Spawn command");
|
||||
done();
|
||||
},150);
|
||||
n1.receive({payload:null});
|
||||
});
|
||||
});
|
||||
});
|
||||
it('should now work if passed multiple words to spawn command', function(done) {
|
||||
var flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"echo this now works", addpay:false, append:"", useSpawn:true},
|
||||
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
||||
helper.load(execNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
var n3 = helper.getNode("n3");
|
||||
var n4 = helper.getNode("n4");
|
||||
n2.on("input", function(msg) {
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.should.be.a.String;
|
||||
msg.payload.should.equal("this now works\n");
|
||||
});
|
||||
n4.on("input", function(msg) {
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.should.be.a.String;
|
||||
msg.payload.should.equal(0);
|
||||
done();
|
||||
});
|
||||
n1.receive({payload:null});
|
||||
});
|
||||
});
|
||||
|
||||
it('should return an error for a bad command', function(done) {
|
||||
var flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"madeupcommandshouldfail", addpay:false, append:"", useSpawn:true},
|
||||
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
||||
helper.load(execNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
var n3 = helper.getNode("n3");
|
||||
var n4 = helper.getNode("n4");
|
||||
n4.on("input", function(msg) {
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.should.be.a.Number;
|
||||
msg.payload.should.be.below(0);
|
||||
done();
|
||||
});
|
||||
n1.receive({payload:null});
|
||||
});
|
||||
});
|
||||
|
||||
it('should return an error for a failing command', function(done) {
|
||||
var flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"mkdir /foo/bar/doo/dah", addpay:false, append:"", useSpawn:true},
|
||||
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
||||
helper.load(execNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
var n3 = helper.getNode("n3");
|
||||
var n4 = helper.getNode("n4");
|
||||
n3.on("input", function(msg) {
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.should.be.a.String;
|
||||
msg.payload.should.equal("mkdir: /foo/bar/doo: No such file or directory\n");
|
||||
});
|
||||
n4.on("input", function(msg) {
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.should.be.a.String;
|
||||
msg.payload.should.equal(1);
|
||||
done();
|
||||
});
|
||||
n1.receive({payload:null});
|
||||
});
|
||||
});
|
||||
|
||||
it('should be able to timeout a long running command', function(done) {
|
||||
var flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"sleep", addpay:false, append:"1", timer:"0.3", useSpawn:true},
|
||||
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
||||
helper.load(execNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
var n3 = helper.getNode("n3");
|
||||
var n4 = helper.getNode("n4");
|
||||
n4.on("input", function(msg) {
|
||||
msg.should.have.property("payload",null);
|
||||
//done();
|
||||
});
|
||||
setTimeout(function() {
|
||||
var logEvents = helper.log().args.filter(function(evt) {
|
||||
return evt[0].type == "exec";
|
||||
});
|
||||
var i = logEvents.length - 1;
|
||||
logEvents[i][0].should.have.a.property('msg');
|
||||
logEvents[i][0].msg.toString().should.startWith("Exec node timeout");
|
||||
done();
|
||||
},400);
|
||||
n1.receive({});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
@@ -135,6 +135,30 @@ describe('function node', function() {
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle null amongst valid messages', function(done) {
|
||||
var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"return [[msg,null,msg],null]"},
|
||||
{id:"n2", type:"helper"},
|
||||
{id:"n3", type:"helper"}];
|
||||
helper.load(functionNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
var n3 = helper.getNode("n3");
|
||||
var n2MsgCount = 0;
|
||||
var n3MsgCount = 0;
|
||||
n2.on("input", function(msg) {
|
||||
n2MsgCount++;
|
||||
});
|
||||
n3.on("input", function(msg) {
|
||||
n3MsgCount++;
|
||||
});
|
||||
n1.receive({payload:"foo",topic: "bar"});
|
||||
setTimeout(function() {
|
||||
n2MsgCount.should.equal(2);
|
||||
n3MsgCount.should.equal(0);
|
||||
done();
|
||||
},100);
|
||||
});
|
||||
});
|
||||
it('should handle and log script error', function(done) {
|
||||
var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"retunr"}];
|
||||
helper.load(functionNode, flow, function() {
|
||||
|
@@ -30,7 +30,7 @@ describe('template node', function() {
|
||||
|
||||
|
||||
it('should modify payload', function(done) {
|
||||
var flow = [{id:"n1", type:"template", field: "payload", template: "payload={{payload}}",wires:[["n2"]]},{id:"n2",type:"helper"}];
|
||||
var flow = [{id:"n1", type:"template", field:"payload", template:"payload={{payload}}",wires:[["n2"]]},{id:"n2",type:"helper"}];
|
||||
helper.load(templateNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
@@ -43,6 +43,50 @@ describe('template node', function() {
|
||||
});
|
||||
});
|
||||
|
||||
it('should modify payload in plain text mode', function(done) {
|
||||
var flow = [{id:"n1", type:"template", field:"payload", syntax:"plain", template:"payload={{payload}}",wires:[["n2"]]},{id:"n2",type:"helper"}];
|
||||
helper.load(templateNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
n2.on("input", function(msg) {
|
||||
msg.should.have.property('topic', 'bar');
|
||||
msg.should.have.property('payload', 'payload={{payload}}');
|
||||
done();
|
||||
});
|
||||
n1.receive({payload:"foo",topic: "bar"});
|
||||
});
|
||||
});
|
||||
|
||||
xit('should modify flow context', function(done) {
|
||||
var flow = [{id:"n1", type:"template", field:"payload", fieldType:"flow", template:"payload={{payload}}",wires:[["n2"]]},{id:"n2",type:"helper"}];
|
||||
helper.load(templateNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
setTimeout( function() {
|
||||
console.log(n2);
|
||||
console.log(n2.context().global.get("payload"));
|
||||
//c.should.equal(1); // should only have had one output.
|
||||
done();
|
||||
},50);
|
||||
n1.receive({payload:"foo",topic: "bar"});
|
||||
});
|
||||
});
|
||||
|
||||
xit('should modify global context', function(done) {
|
||||
var flow = [{id:"n1", type:"template", field:"payload", fieldType:"global", template:"payload={{payload}}",wires:[["n2"]]},{id:"n2",type:"helper"}];
|
||||
helper.load(templateNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
setTimeout( function() {
|
||||
console.log(n2);
|
||||
console.log(n2.context().global.get("payload"));
|
||||
//c.should.equal(1); // should only have had one output.
|
||||
done();
|
||||
},50);
|
||||
n1.receive({payload:"foo",topic: "bar"});
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle if the field isn\'t set', function(done) {
|
||||
var flow = [{id:"n1", type:"template", template: "payload={{payload}}",wires:[["n2"]]},{id:"n2",type:"helper"}];
|
||||
helper.load(templateNode, flow, function() {
|
||||
|
@@ -28,7 +28,6 @@ var secondsToMinutes = 60;
|
||||
var secondsToHours = 3600;
|
||||
var secondsToDays = 86400;
|
||||
|
||||
|
||||
describe('delay Node', function() {
|
||||
|
||||
beforeEach(function(done) {
|
||||
@@ -71,11 +70,11 @@ describe('delay Node', function() {
|
||||
});
|
||||
|
||||
var TimeUnitEnum = {
|
||||
MILLIS : "milliseconds",
|
||||
SECONDS : "seconds",
|
||||
MINUTES : "minutes",
|
||||
HOURS : "hours",
|
||||
DAYS : "days"
|
||||
MILLIS : "milliseconds",
|
||||
SECONDS : "seconds",
|
||||
MINUTES : "minutes",
|
||||
HOURS : "hours",
|
||||
DAYS : "days"
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -90,7 +89,7 @@ describe('delay Node', function() {
|
||||
var minExpected = expectedValue - toleranceFraction;
|
||||
var maxExpected = expectedValue + toleranceFraction;
|
||||
|
||||
if(actualValue >= minExpected && actualValue <= maxExpected) {
|
||||
if (actualValue >= minExpected && actualValue <= maxExpected) {
|
||||
toReturn = true;
|
||||
} else {
|
||||
toReturn = false;
|
||||
@@ -117,23 +116,23 @@ describe('delay Node', function() {
|
||||
var aTimeoutUnifiedToSeconds;
|
||||
|
||||
// calculating the timeout in seconds
|
||||
if(aTimeoutUnit == TimeUnitEnum.MILLIS) {
|
||||
if (aTimeoutUnit == TimeUnitEnum.MILLIS) {
|
||||
aTimeoutUnifiedToSeconds = aTimeout / millisToSeconds;
|
||||
} else if(aTimeoutUnit == TimeUnitEnum.SECONDS) {
|
||||
} else if (aTimeoutUnit == TimeUnitEnum.SECONDS) {
|
||||
aTimeoutUnifiedToSeconds = aTimeout;
|
||||
} else if(aTimeoutUnit == TimeUnitEnum.MINUTES) {
|
||||
} else if (aTimeoutUnit == TimeUnitEnum.MINUTES) {
|
||||
aTimeoutUnifiedToSeconds = aTimeout * secondsToMinutes;
|
||||
} else if(aTimeoutUnit == TimeUnitEnum.HOURS) {
|
||||
} else if (aTimeoutUnit == TimeUnitEnum.HOURS) {
|
||||
aTimeoutUnifiedToSeconds = aTimeout * secondsToHours;
|
||||
} else if(aTimeoutUnit == TimeUnitEnum.DAYS) {
|
||||
} else if (aTimeoutUnit == TimeUnitEnum.DAYS) {
|
||||
aTimeoutUnifiedToSeconds = aTimeout * secondsToDays;
|
||||
}
|
||||
|
||||
if(closeEnough(runtimeSeconds, aTimeoutUnifiedToSeconds, GRACE_PERCENTAGE)) {
|
||||
if (closeEnough(runtimeSeconds, aTimeoutUnifiedToSeconds, GRACE_PERCENTAGE)) {
|
||||
done();
|
||||
} else {
|
||||
try {
|
||||
should.fail(null, null, "Delayed runtime seconds " + runtimeSeconds + " was not close enough to exlected timeout seconds: " + aTimeoutUnifiedToSeconds);
|
||||
should.fail(null, null, "Delayed runtime seconds " + runtimeSeconds + " was not close enough to exlected timeout seconds: " + aTimeoutUnifiedToSeconds);
|
||||
} catch (err) {
|
||||
done(err);
|
||||
}
|
||||
@@ -188,7 +187,7 @@ describe('delay Node', function() {
|
||||
var receiveTimestamp;
|
||||
|
||||
helperNode1.on("input", function(msg) {
|
||||
if(receiveTimestamp) {
|
||||
if (receiveTimestamp) {
|
||||
var elapse = process.hrtime(receiveTimestamp);
|
||||
var receiveInterval = (elapse[0] * 1000) + ((elapse[1] / nanosToSeconds) * 1000);
|
||||
receiveInterval.should.be.above(rate * 0.9);
|
||||
@@ -200,16 +199,16 @@ describe('delay Node', function() {
|
||||
var possibleMaxMessageCount = Math.ceil(aLimit * (runtimeInMillis / 1000) + aLimit); // +aLimit as at the start of the 2nd period, we're allowing the 3rd burst
|
||||
|
||||
var i = 0;
|
||||
for(; i < possibleMaxMessageCount + 1; i++) {
|
||||
for (; i < possibleMaxMessageCount + 1; i++) {
|
||||
delayNode1.receive({payload:i});
|
||||
}
|
||||
|
||||
setTimeout(function() {
|
||||
try {
|
||||
receivedMessagesStack.length.should.be.lessThan(possibleMaxMessageCount);
|
||||
for(var j = 0; j < receivedMessagesStack.length; j++) {
|
||||
if(receivedMessagesStack[j].payload === j) {
|
||||
if(j === (receivedMessagesStack.length -1)) { // last message, all matched so far
|
||||
for (var j = 0; j < receivedMessagesStack.length; j++) {
|
||||
if (receivedMessagesStack[j].payload === j) {
|
||||
if (j === (receivedMessagesStack.length -1)) { // last message, all matched so far
|
||||
done();
|
||||
}
|
||||
} else {
|
||||
@@ -250,7 +249,7 @@ describe('delay Node', function() {
|
||||
var receiveTimestamp;
|
||||
|
||||
helperNode1.on("input", function(msg) {
|
||||
if(receiveTimestamp) {
|
||||
if (receiveTimestamp) {
|
||||
var elapse = process.hrtime(receiveTimestamp);
|
||||
var receiveInterval = (elapse[0] * 1000) + ((elapse[1] / nanosToSeconds) * 1000);
|
||||
receiveInterval.should.be.above(rate * 0.9);
|
||||
@@ -264,9 +263,9 @@ describe('delay Node', function() {
|
||||
var i = 0;
|
||||
delayNode1.receive({payload:i});
|
||||
i++;
|
||||
for(; i < possibleMaxMessageCount + 1; i++) {
|
||||
for (; i < possibleMaxMessageCount + 1; i++) {
|
||||
setTimeout(function() {
|
||||
delayNode1.receive({payload:i});
|
||||
delayNode1.receive({payload:i});
|
||||
}, 2 * ((rate * i) / possibleMaxMessageCount) );
|
||||
}
|
||||
|
||||
@@ -281,9 +280,9 @@ describe('delay Node', function() {
|
||||
receivedMessagesStack.length.should.be.greaterThan(2); // ensure that we receive more than 1st and last message
|
||||
receivedMessagesStack[0].payload.should.be.exactly(0); // means we received the last message injected just before test termination
|
||||
var foundAtLeastOneDrop = false;
|
||||
for(var i = 0; i < receivedMessagesStack.length; i++) {
|
||||
if(i > 0) {
|
||||
if(receivedMessagesStack[i].payload - receivedMessagesStack[i - 1].payload > 1) {
|
||||
for (var i = 0; i < receivedMessagesStack.length; i++) {
|
||||
if (i > 0) {
|
||||
if (receivedMessagesStack[i].payload - receivedMessagesStack[i - 1].payload > 1) {
|
||||
foundAtLeastOneDrop = true;
|
||||
}
|
||||
}
|
||||
@@ -317,11 +316,11 @@ describe('delay Node', function() {
|
||||
* @param allowedGracePercent - The percentage of grace allowed
|
||||
*/
|
||||
function inBetweenDelays(timeoutFrom, timeoutTo, actualTimeout, allowedGracePercent) {
|
||||
if(closeEnough(actualTimeout, timeoutFrom, allowedGracePercent)) {
|
||||
if (closeEnough(actualTimeout, timeoutFrom, allowedGracePercent)) {
|
||||
return true;
|
||||
} else if(closeEnough(actualTimeout, timeoutTo, allowedGracePercent)) {
|
||||
} else if (closeEnough(actualTimeout, timeoutTo, allowedGracePercent)) {
|
||||
return true;
|
||||
} else if(timeoutFrom < actualTimeout && timeoutTo > actualTimeout) {
|
||||
} else if (timeoutFrom < actualTimeout && timeoutTo > actualTimeout) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
@@ -349,28 +348,28 @@ describe('delay Node', function() {
|
||||
var aTimeoutToUnifiedToSeconds;
|
||||
|
||||
// calculating the timeout in seconds
|
||||
if(aTimeoutUnit == TimeUnitEnum.MILLIS) {
|
||||
if (aTimeoutUnit == TimeUnitEnum.MILLIS) {
|
||||
aTimeoutFromUnifiedToSeconds = aTimeoutFrom / millisToSeconds;
|
||||
aTimeoutToUnifiedToSeconds = aTimeoutTo / millisToSeconds;
|
||||
} else if(aTimeoutUnit == TimeUnitEnum.SECONDS) {
|
||||
} else if (aTimeoutUnit == TimeUnitEnum.SECONDS) {
|
||||
aTimeoutFromUnifiedToSeconds = aTimeoutFrom;
|
||||
aTimeoutToUnifiedToSeconds = aTimeoutTo;
|
||||
} else if(aTimeoutUnit == TimeUnitEnum.MINUTES) {
|
||||
} else if (aTimeoutUnit == TimeUnitEnum.MINUTES) {
|
||||
aTimeoutFromUnifiedToSeconds = aTimeoutFrom * secondsToMinutes;
|
||||
aTimeoutToUnifiedToSeconds = aTimeoutTo * secondsToMinutes;
|
||||
} else if(aTimeoutUnit == TimeUnitEnum.HOURS) {
|
||||
} else if (aTimeoutUnit == TimeUnitEnum.HOURS) {
|
||||
aTimeoutFromUnifiedToSeconds = aTimeoutFrom * secondsToHours;
|
||||
aTimeoutToUnifiedToSeconds = aTimeoutTo * secondsToHours;
|
||||
} else if(aTimeoutUnit == TimeUnitEnum.DAYS) {
|
||||
} else if (aTimeoutUnit == TimeUnitEnum.DAYS) {
|
||||
aTimeoutFromUnifiedToSeconds = aTimeoutFrom * secondsToDays;
|
||||
aTimeoutToUnifiedToSeconds = aTimeoutTo * secondsToDays;
|
||||
}
|
||||
|
||||
if(inBetweenDelays(aTimeoutFromUnifiedToSeconds, aTimeoutToUnifiedToSeconds, runtimeSeconds, GRACE_PERCENTAGE)) {
|
||||
if (inBetweenDelays(aTimeoutFromUnifiedToSeconds, aTimeoutToUnifiedToSeconds, runtimeSeconds, GRACE_PERCENTAGE)) {
|
||||
done();
|
||||
} else {
|
||||
try {
|
||||
should.fail(null, null, "Delayed runtime seconds " + runtimeSeconds + " was not \"in between enough\" enough to expected values of: " + aTimeoutFromUnifiedToSeconds + " and " + aTimeoutToUnifiedToSeconds);
|
||||
should.fail(null, null, "Delayed runtime seconds " + runtimeSeconds + " was not \"in between enough\" enough to expected values of: " + aTimeoutFromUnifiedToSeconds + " and " + aTimeoutToUnifiedToSeconds);
|
||||
} catch (err) {
|
||||
done(err);
|
||||
}
|
||||
@@ -404,118 +403,119 @@ describe('delay Node', function() {
|
||||
randomDelayTest(0.0000046296, 0.0000092593, "days", done);
|
||||
});
|
||||
|
||||
it.skip('handles bursts using a buffer', function(done) {
|
||||
// routinely timesout on Travis - needs fixing
|
||||
this.timeout(8000);
|
||||
it('handles bursts using a buffer', function(done) {
|
||||
this.timeout(10000);
|
||||
|
||||
var flow = [{"id":"delayNode1","type":"delay","name":"delayNode","pauseType":"rate","timeout":5,"timeoutUnits":"seconds","rate":1000,"rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"wires":[["helperNode1"]]},
|
||||
{id:"helperNode1", type:"helper", wires:[]}];
|
||||
helper.load(delayNode, flow, function() {
|
||||
var delayNode1 = helper.getNode("delayNode1");
|
||||
var helperNode1 = helper.getNode("helperNode1");
|
||||
|
||||
var sinon = require('sinon');
|
||||
|
||||
var receivedWarning = false;
|
||||
var messageBurstSize = 1500;
|
||||
var messageBurstSize = 1200;
|
||||
|
||||
// we ensure that we note that a warning is received for buffer growth
|
||||
sinon.stub(delayNode1, 'warn', function(warning){
|
||||
if(warning.indexOf("buffer exceeded 1000 messages" > -1)) {
|
||||
sinon.stub(delayNode1, 'warn', function(warning) {
|
||||
if (warning.indexOf("buffer exceeded 1000 messages" > -1)) {
|
||||
receivedWarning = true;
|
||||
}
|
||||
});
|
||||
|
||||
// we ensure that the warning is received for buffer size and that we get the last message
|
||||
helperNode1.on("input", function(msg) {
|
||||
if(msg.payload === (messageBurstSize - 1) && receivedWarning === true) {
|
||||
if (msg.payload === (messageBurstSize - 1) && receivedWarning === true) {
|
||||
done(); // it will timeout if we don't receive the last message
|
||||
}
|
||||
});
|
||||
// send 1500 messages as quickly as possible
|
||||
for(var i = 0; i < messageBurstSize; i++) {
|
||||
// send messages as quickly as possible
|
||||
for (var i = 0; i < messageBurstSize; i++) {
|
||||
delayNode1.receive({payload:i});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('handles delay queue', function(done) {
|
||||
this.timeout(6000);
|
||||
|
||||
var flow = [{"id":"delayNode1","type":"delay","name":"delayNode","pauseType":"queue","timeout":5,"timeoutUnits":"seconds","rate":1000,"rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"wires":[["helperNode1"]]},
|
||||
this.timeout(2000);
|
||||
var flow = [{id:"delayNode1", type :"delay","name":"delayNode","pauseType":"queue","timeout":1,"timeoutUnits":"seconds","rate":4,"rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"wires":[["helperNode1"]]},
|
||||
{id:"helperNode1", type:"helper", wires:[]}];
|
||||
helper.load(delayNode, flow, function() {
|
||||
var delayNode1 = helper.getNode("delayNode1");
|
||||
var helperNode1 = helper.getNode("helperNode1");
|
||||
var messages = 2;
|
||||
var c = 0;
|
||||
|
||||
var t = Date.now();
|
||||
helperNode1.on("input", function(msg) {
|
||||
c += 1;
|
||||
msg.should.have.a.property('payload');
|
||||
msg.should.have.a.property('topic');
|
||||
if (msg.topic === "A") {
|
||||
msg.payload.should.equal(4);
|
||||
}
|
||||
else {
|
||||
msg.topic.should.equal("_none_");
|
||||
msg.payload.should.equal(2);
|
||||
}
|
||||
if (c == 2) {
|
||||
done(); // it will timeout if we don't receive both messages
|
||||
try {
|
||||
if (msg.topic === "_none_") {
|
||||
msg.payload.should.equal(2);
|
||||
(Date.now() - t).should.be.approximately(500,50);
|
||||
}
|
||||
else if (msg.topic === "A") {
|
||||
msg.payload.should.equal(4);
|
||||
(Date.now() - t).should.be.approximately(750,50);
|
||||
}
|
||||
else {
|
||||
msg.topic.should.equal("B");
|
||||
msg.payload.should.equal(1);
|
||||
(Date.now() - t).should.be.approximately(1000,50);
|
||||
done();
|
||||
}
|
||||
} catch(e) {
|
||||
done(e);
|
||||
}
|
||||
});
|
||||
|
||||
// send test messages
|
||||
delayNode1.receive({payload:1,topic:"A"});
|
||||
delayNode1.receive({payload:1});
|
||||
delayNode1.receive({payload:2,topic:"A"});
|
||||
delayNode1.receive({payload:3,topic:"A"});
|
||||
delayNode1.receive({payload:2}); // only this should get out
|
||||
delayNode1.receive({payload:4,topic:"A"}); // and this one also
|
||||
|
||||
setTimeout(function() {
|
||||
// send test messages
|
||||
delayNode1.receive({payload:1}); // send something with blank topic
|
||||
delayNode1.receive({payload:1,topic:"A"}); // and something with a fixed topic
|
||||
delayNode1.receive({payload:1,topic:"B"}); // and something else with a fixed topic (3rd tick)
|
||||
delayNode1.receive({payload:2,topic:"A"}); // these should replace them in queue
|
||||
delayNode1.receive({payload:3,topic:"A"}); // ditto
|
||||
delayNode1.receive({payload:2}); // so only this should get out on first tick
|
||||
delayNode1.receive({payload:4,topic:"A"}); // and this one on second tick
|
||||
}, 275); // wait one tick beofre starting.. (to test no messages in queue path.)
|
||||
});
|
||||
});
|
||||
|
||||
it('handles timed queue', function(done) {
|
||||
this.timeout(6000);
|
||||
|
||||
var flow = [{"id":"delayNode1","type":"delay","name":"delayNode","pauseType":"timed","timeout":5,"timeoutUnits":"seconds","rate":1000,"rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"wires":[["helperNode1"]]},
|
||||
this.timeout(2000);
|
||||
var flow = [{"id":"delayNode1","type":"delay","name":"delayNode","pauseType":"timed","timeout":1,"timeoutUnits":"seconds","rate":2,"rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"wires":[["helperNode1"]]},
|
||||
{id:"helperNode1", type:"helper", wires:[]}];
|
||||
helper.load(delayNode, flow, function() {
|
||||
var delayNode1 = helper.getNode("delayNode1");
|
||||
var helperNode1 = helper.getNode("helperNode1");
|
||||
var messages = 2;
|
||||
var c = 0;
|
||||
|
||||
var t = Date.now();
|
||||
helperNode1.on("input", function(msg) {
|
||||
c += 1;
|
||||
if (c === 1) {
|
||||
msg.should.have.property("topic","A");
|
||||
msg.should.have.property("payload",3);
|
||||
}
|
||||
else if (c === 2) {
|
||||
msg.should.have.property("topic","_none_");
|
||||
msg.should.have.property("payload",2);
|
||||
}
|
||||
else if (c === 3) {
|
||||
msg.should.have.property("topic","_none_");
|
||||
msg.should.have.property("payload","Biscuit");
|
||||
done();
|
||||
msg.should.have.a.property('payload');
|
||||
msg.should.have.a.property('topic');
|
||||
try {
|
||||
if (msg.topic === "_none_") {
|
||||
msg.payload.should.equal(2);
|
||||
(Date.now() - t).should.be.approximately(500,50);
|
||||
}
|
||||
else if (msg.topic === "A") {
|
||||
msg.payload.should.equal(4);
|
||||
(Date.now() - t).should.be.approximately(500,50);
|
||||
}
|
||||
else {
|
||||
msg.topic.should.equal("B");
|
||||
msg.payload.should.equal(1);
|
||||
(Date.now() - t).should.be.approximately(500,50);
|
||||
done();
|
||||
}
|
||||
} catch(e) {
|
||||
done(e);
|
||||
}
|
||||
});
|
||||
|
||||
// send test messages
|
||||
delayNode1.receive({payload:1,topic:"A"});
|
||||
delayNode1.receive({payload:1});
|
||||
delayNode1.receive({payload:2,topic:"A"});
|
||||
delayNode1.receive({payload:3,topic:"A"}); // should get this
|
||||
delayNode1.receive({payload:2}); // and this
|
||||
setTimeout( function() {
|
||||
delayNode1.receive({payload:"Biscuit"}); // and then this
|
||||
},2000);
|
||||
|
||||
delayNode1.receive({payload:1}); // send something with blank topic
|
||||
delayNode1.receive({payload:1,topic:"A"}); // and something with a fixed topic
|
||||
delayNode1.receive({payload:1,topic:"B"}); // and something else with a fixed topic
|
||||
delayNode1.receive({payload:2,topic:"A"}); // these should replace them in queue
|
||||
delayNode1.receive({payload:3,topic:"A"}); // ditto
|
||||
delayNode1.receive({payload:2}); // so all should go on first tick
|
||||
delayNode1.receive({payload:4,topic:"A"}); // and nothing on second
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
@@ -235,6 +235,64 @@ describe('trigger Node', function() {
|
||||
});
|
||||
});
|
||||
|
||||
it('should be able to extend the delay and output the 2nd payload', function(done) {
|
||||
var flow = [{"id":"n1", "type":"trigger", "name":"triggerNode", extend:"true", op1type:"nul", op2type:"payl", op1:"false", op2:"true", duration:200, wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(triggerNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
var c = 0;
|
||||
n2.on("input", function(msg) {
|
||||
if (c === 0) {
|
||||
msg.should.have.a.property("payload", "Goodbye");
|
||||
c += 1;
|
||||
}
|
||||
else {
|
||||
msg.should.have.a.property("payload", "World");
|
||||
(Date.now() - ss).should.be.greaterThan(380);
|
||||
done();
|
||||
}
|
||||
});
|
||||
var ss = Date.now();
|
||||
n1.emit("input", {payload:"Hello"});
|
||||
setTimeout( function() {
|
||||
n1.emit("input", {payload:"Goodbye"});
|
||||
},100);
|
||||
setTimeout( function() {
|
||||
n1.emit("input", {payload:"World"});
|
||||
},400);
|
||||
});
|
||||
});
|
||||
|
||||
it('should be able output the 2nd payload', function(done) {
|
||||
var flow = [{"id":"n1", "type":"trigger", "name":"triggerNode", extend:"false", op1type:"nul", op2type:"payl", op1:"false", op2:"true", duration:200, wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(triggerNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
var c = 0;
|
||||
n2.on("input", function(msg) {
|
||||
if (c === 0) {
|
||||
msg.should.have.a.property("payload", "Goodbye");
|
||||
c += 1;
|
||||
}
|
||||
else {
|
||||
msg.should.have.a.property("payload", "World");
|
||||
(Date.now() - ss).should.be.greaterThan(380);
|
||||
done();
|
||||
}
|
||||
});
|
||||
var ss = Date.now();
|
||||
n1.emit("input", {payload:"Hello"});
|
||||
setTimeout( function() {
|
||||
n1.emit("input", {payload:"Goodbye"});
|
||||
},100);
|
||||
setTimeout( function() {
|
||||
n1.emit("input", {payload:"World"});
|
||||
},400);
|
||||
});
|
||||
});
|
||||
|
||||
it('should be able to apply mustache templates to payloads', function(done) {
|
||||
var flow = [{"id":"n1", "type":"trigger", "name":"triggerNode", op1type:"val", op2type:"val", op1:"{{payload}}", op2:"{{topic}}", duration:50, wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
@@ -257,7 +315,7 @@ describe('trigger Node', function() {
|
||||
});
|
||||
|
||||
it('should handle string null as null', function(done) {
|
||||
var flow = [{"id":"n1", "type":"trigger", "name":"triggerNode", op2type:"pay", op1:"null", op2:"null", duration:30, wires:[["n2"]] },
|
||||
var flow = [{"id":"n1", "type":"trigger", "name":"triggerNode", op1type:"val", op2type:"pay", op1:"null", op2:"null", duration:40, wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(triggerNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
@@ -285,13 +343,39 @@ describe('trigger Node', function() {
|
||||
var n2 = helper.getNode("n2");
|
||||
var c = 0;
|
||||
n2.on("input", function(msg) {
|
||||
c += 1;
|
||||
msg.should.have.a.property("payload", 1);
|
||||
});
|
||||
setTimeout( function() {
|
||||
n1.emit("input", {reset:true});
|
||||
done();
|
||||
},500);
|
||||
n1.emit("input", {payload:null});
|
||||
if (c === 2) { done(); }
|
||||
},100);
|
||||
n1.emit("input", {payload:null}); // trigger
|
||||
n1.emit("input", {payload:null}); // blocked
|
||||
n1.emit("input", {payload:null}); // blocked
|
||||
n1.emit("input", {reset:true}); // clear the blockage
|
||||
n1.emit("input", {payload:null}); // trigger
|
||||
});
|
||||
});
|
||||
|
||||
it('should be able to set infinite timeout, and clear timeout by message', function(done) {
|
||||
var flow = [{"id":"n1", "type":"trigger", "name":"triggerNode", reset:"boo", duration:-5, wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(triggerNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
var c = 0;
|
||||
n2.on("input", function(msg) {
|
||||
c += 1;
|
||||
msg.should.have.a.property("payload", 1);
|
||||
});
|
||||
setTimeout( function() {
|
||||
if (c === 2) { done(); }
|
||||
},100);
|
||||
n1.emit("input", {payload:null}); // trigger
|
||||
n1.emit("input", {payload:null}); // blocked
|
||||
n1.emit("input", {payload:null}); // blocked
|
||||
n1.emit("input", {payload:"boo"}); // clear the blockage
|
||||
n1.emit("input", {payload:null}); // trigger
|
||||
});
|
||||
});
|
||||
|
||||
|
@@ -100,7 +100,7 @@ describe('switch Node', function() {
|
||||
var helperNode1 = helper.getNode("helperNode1");
|
||||
helperNode1.on("input", function(msg) {
|
||||
try {
|
||||
if(shouldReceive === true) {
|
||||
if (shouldReceive === true) {
|
||||
msg.payload.should.equal(sendPayload);
|
||||
done();
|
||||
} else {
|
||||
@@ -111,7 +111,7 @@ describe('switch Node', function() {
|
||||
}
|
||||
});
|
||||
switchNode1.receive({payload:sendPayload});
|
||||
if(shouldReceive === false) {
|
||||
if (shouldReceive === false) {
|
||||
setTimeout(function() {
|
||||
done();
|
||||
}, 200);
|
||||
@@ -184,7 +184,7 @@ describe('switch Node', function() {
|
||||
});
|
||||
|
||||
it('should check if payload is between given values', function(done) {
|
||||
twoFieldSwitchTest("btwn", 3, 5, true, true, 4, done);
|
||||
twoFieldSwitchTest("btwn", "3", "5", true, true, 4, done);
|
||||
});
|
||||
|
||||
it('should check if payload is not between given values', function(done) {
|
||||
@@ -243,18 +243,25 @@ describe('switch Node', function() {
|
||||
singularSwitchTest(false, true, false, true, done);
|
||||
});
|
||||
|
||||
it('should check if input is indeed null', function(done) {
|
||||
var flow = [{id:"switchNode1",type:"switch",name:"switchNode",property:"payload",rules:[{"t":"null"}],checkall:true,outputs:1,wires:[["helperNode1"]]},
|
||||
it('should check input against a previous value', function(done) {
|
||||
var flow = [{id:"switchNode1",type:"switch",name:"switchNode",property:"payload",rules:[{ "t": "gt", "v": "", "vt": "prev" }],checkall:true,outputs:1,wires:[["helperNode1"]]},
|
||||
{id:"helperNode1", type:"helper", wires:[]}];
|
||||
|
||||
|
||||
helper.load(switchNode, flow, function() {
|
||||
var switchNode1 = helper.getNode("switchNode1");
|
||||
var helperNode1 = helper.getNode("helperNode1");
|
||||
var c = 0;
|
||||
helperNode1.on("input", function(msg) {
|
||||
if(msg.payload) {
|
||||
if (msg.payload) {
|
||||
try {
|
||||
should.fail(null, null, "msg.payload should be undefined!");
|
||||
if (c === 0) {
|
||||
msg.payload.should.equal(1);
|
||||
}
|
||||
if (c === 1) {
|
||||
msg.payload.should.equal(2);
|
||||
done();
|
||||
}
|
||||
c += 1;
|
||||
} catch (err) {
|
||||
done(err);
|
||||
}
|
||||
@@ -262,6 +269,81 @@ describe('switch Node', function() {
|
||||
done();
|
||||
}
|
||||
});
|
||||
switchNode1.receive({payload:1});
|
||||
switchNode1.receive({payload:0});
|
||||
switchNode1.receive({payload:-2});
|
||||
switchNode1.receive({payload:2});
|
||||
});
|
||||
});
|
||||
|
||||
it('should check input against a previous value (2nd option)', function(done) {
|
||||
var flow = [{id:"switchNode1",type:"switch",name:"switchNode",property:"payload",rules:[{"t": "btwn", "v": "10", "vt": "num", "v2": "", "v2t": "prev" }],checkall:true,outputs:1,wires:[["helperNode1"]]},
|
||||
{id:"helperNode1", type:"helper", wires:[]}];
|
||||
|
||||
helper.load(switchNode, flow, function() {
|
||||
var switchNode1 = helper.getNode("switchNode1");
|
||||
var helperNode1 = helper.getNode("helperNode1");
|
||||
var c = 0;
|
||||
helperNode1.on("input", function(msg) {
|
||||
if (msg.payload) {
|
||||
try {
|
||||
if (c === 0) {
|
||||
msg.payload.should.equal(20);
|
||||
}
|
||||
if (c === 1) {
|
||||
msg.payload.should.equal(25);
|
||||
done();
|
||||
}
|
||||
c += 1;
|
||||
} catch (err) {
|
||||
done(err);
|
||||
}
|
||||
} else {
|
||||
done();
|
||||
}
|
||||
});
|
||||
switchNode1.receive({payload:0});
|
||||
switchNode1.receive({payload:20}); // between 10 and 0
|
||||
switchNode1.receive({payload:30}); // between 10 and 20
|
||||
switchNode1.receive({payload:20}); // between 10 and 30 yes
|
||||
switchNode1.receive({payload:30}); // between 10 and 20 no
|
||||
switchNode1.receive({payload:25}); // between 10 and 30 yes
|
||||
});
|
||||
});
|
||||
|
||||
it('should check if input is indeed null', function(done) {
|
||||
var flow = [{id:"switchNode1",type:"switch",name:"switchNode",property:"payload",rules:[{"t":"null"}],checkall:true,outputs:1,wires:[["helperNode1"]]},
|
||||
{id:"helperNode1", type:"helper", wires:[]}];
|
||||
|
||||
helper.load(switchNode, flow, function() {
|
||||
var switchNode1 = helper.getNode("switchNode1");
|
||||
var helperNode1 = helper.getNode("helperNode1");
|
||||
helperNode1.on("input", function(msg) {
|
||||
if (msg.payload === null) {
|
||||
done();
|
||||
} else {
|
||||
console.log("msg is ",msg);
|
||||
}
|
||||
});
|
||||
switchNode1.receive({payload:null});
|
||||
});
|
||||
});
|
||||
|
||||
it('should check if input is indeed undefined', function(done) {
|
||||
var flow = [{id:"switchNode1",type:"switch",name:"switchNode",property:"payload",rules:[{"t":"null"}],checkall:true,outputs:1,wires:[["helperNode1"]]},
|
||||
{id:"helperNode1", type:"helper", wires:[]}];
|
||||
|
||||
helper.load(switchNode, flow, function() {
|
||||
var switchNode1 = helper.getNode("switchNode1");
|
||||
var helperNode1 = helper.getNode("helperNode1");
|
||||
helperNode1.on("input", function(msg) {
|
||||
if (msg.payload === undefined) {
|
||||
done();
|
||||
}
|
||||
else {
|
||||
console.log("msg is ",msg);
|
||||
}
|
||||
});
|
||||
switchNode1.receive({payload:undefined});
|
||||
});
|
||||
});
|
||||
@@ -275,7 +357,7 @@ describe('switch Node', function() {
|
||||
var switchNode1 = helper.getNode("switchNode1");
|
||||
var helperNode1 = helper.getNode("helperNode1");
|
||||
helperNode1.on("input", function(msg) {
|
||||
if(msg.payload) {
|
||||
if (msg.payload) {
|
||||
done();
|
||||
} else {
|
||||
try {
|
||||
@@ -319,7 +401,7 @@ describe('switch Node', function() {
|
||||
try {
|
||||
msg.payload.should.equal("Hello");
|
||||
nodeHitCount++;
|
||||
if(nodeHitCount == 2) {
|
||||
if (nodeHitCount == 2) {
|
||||
done();
|
||||
} else {
|
||||
try {
|
||||
@@ -349,7 +431,6 @@ describe('switch Node', function() {
|
||||
{id:"helperNode2", type:"helper", wires:[]},
|
||||
{id:"helperNode3", type:"helper", wires:[]}];
|
||||
|
||||
|
||||
helper.load(switchNode, flow, function() {
|
||||
var switchNode1 = helper.getNode("switchNode1");
|
||||
var helperNode1 = helper.getNode("helperNode1");
|
||||
|
@@ -242,7 +242,6 @@ describe('change Node', function() {
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
it('changes the value to a number', function(done) {
|
||||
var flow = [{"id":"changeNode1","type":"change",rules:[{"t":"set","p":"payload","to":"123","tot":"num"}],"name":"changeNode","wires":[["helperNode1"]]},
|
||||
{id:"helperNode1", type:"helper", wires:[]}];
|
||||
@@ -260,6 +259,7 @@ describe('change Node', function() {
|
||||
changeNode1.receive({payload:""});
|
||||
});
|
||||
});
|
||||
|
||||
it('changes the value to a js object', function(done) {
|
||||
var flow = [{"id":"changeNode1","type":"change",rules:[{"t":"set","p":"payload","to":'{"a":123}',"tot":"json"}],"name":"changeNode","wires":[["helperNode1"]]},
|
||||
{id:"helperNode1", type:"helper", wires:[]}];
|
||||
@@ -278,6 +278,24 @@ describe('change Node', function() {
|
||||
});
|
||||
});
|
||||
|
||||
it('sets the value of the message property to the current timestamp', function(done) {
|
||||
var flow = [{"id":"changeNode1","type":"change","rules":[{"t":"set","p":"ts","pt":"msg","to":"","tot":"date"}],"name":"changeNode","wires":[["helperNode1"]]},
|
||||
{id:"helperNode1", type:"helper", wires:[]}];
|
||||
helper.load(changeNode, flow, function() {
|
||||
var changeNode1 = helper.getNode("changeNode1");
|
||||
var helperNode1 = helper.getNode("helperNode1");
|
||||
var rule = helper.getNode("changeNode1").rules[0];
|
||||
helperNode1.on("input", function(msg) {
|
||||
try {
|
||||
(Date.now() - msg.ts).should.be.approximately(0,50);
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
changeNode1.receive({payload:Date.now()});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
describe('#change', function() {
|
||||
@@ -608,8 +626,53 @@ describe('change Node', function() {
|
||||
});
|
||||
});
|
||||
});
|
||||
describe('- multiple rules', function() {
|
||||
|
||||
//[{"id":"d5df2b27.7443a8","type":"change","z":"b0e25b28.3c7e88","name":"","rules":[{"t":"move","p":"topic","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":242.5,"y":541,"wires":[[]]}]
|
||||
|
||||
describe("#move", function() {
|
||||
it('moves the value of the message property', function(done) {
|
||||
var flow = [{"id":"changeNode1","type":"change","rules":[{"t":"move","p":"topic","pt":"msg","to":"payload","tot":"msg"}],"name":"changeNode","wires":[["helperNode1"]]},
|
||||
{id:"helperNode1", type:"helper", wires:[]}];
|
||||
helper.load(changeNode, flow, function() {
|
||||
var changeNode1 = helper.getNode("changeNode1");
|
||||
var helperNode1 = helper.getNode("helperNode1");
|
||||
helperNode1.on("input", function(msg) {
|
||||
try {
|
||||
msg.should.not.have.property('topic');
|
||||
msg.should.have.property('payload');
|
||||
msg.payload.should.equal("You've got to move it move it.");
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
changeNode1.receive({topic:"You've got to move it move it.", payload:{foo:"bar"}});
|
||||
});
|
||||
});
|
||||
it('moves the value of a message property object', function(done) {
|
||||
var flow = [{"id":"changeNode1","type":"change","rules":[{"t":"move","p":"topic","pt":"msg","to":"payload","tot":"msg"}],"name":"changeNode","wires":[["helperNode1"]]},
|
||||
{id:"helperNode1", type:"helper", wires:[]}];
|
||||
helper.load(changeNode, flow, function() {
|
||||
var changeNode1 = helper.getNode("changeNode1");
|
||||
var helperNode1 = helper.getNode("helperNode1");
|
||||
helperNode1.on("input", function(msg) {
|
||||
try {
|
||||
msg.should.not.have.property('topic');
|
||||
msg.should.have.property('payload');
|
||||
msg.payload.should.have.property('foo');
|
||||
msg.payload.foo.should.have.property('bar');
|
||||
msg.payload.foo.bar.should.equal(1);
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
changeNode1.receive({topic:{foo:{bar:1}}, payload:"String"});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('- multiple rules', function() {
|
||||
it('handles multiple rules', function(done) {
|
||||
var flow = [{"id":"changeNode1","type":"change","wires":[["helperNode1"]],
|
||||
rules:[
|
||||
@@ -662,6 +725,5 @@ describe('change Node', function() {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
385
test/nodes/core/logic/17-split_spec.js
Normal file
385
test/nodes/core/logic/17-split_spec.js
Normal file
@@ -0,0 +1,385 @@
|
||||
/**
|
||||
* Copyright 2016 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 splitNode = require("../../../../nodes/core/logic/17-split.js");
|
||||
var joinNode = require("../../../../nodes/core/logic/17-split.js");
|
||||
var helper = require("../../helper.js");
|
||||
|
||||
describe('SPLIT node', function() {
|
||||
|
||||
before(function(done) {
|
||||
helper.startServer(done);
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
helper.unload();
|
||||
});
|
||||
|
||||
it('should be loaded', function(done) {
|
||||
var flow = [{id:"splitNode1", type:"split", name:"splitNode" }];
|
||||
helper.load(splitNode, flow, function() {
|
||||
var splitNode1 = helper.getNode("splitNode1");
|
||||
splitNode1.should.have.property('name', 'splitNode');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should split an array into multiple messages', function(done) {
|
||||
var flow = [{id:"sn1", type:"split", wires:[["sn2"]]},
|
||||
{id:"sn2", type:"helper"}];
|
||||
helper.load(splitNode, flow, function() {
|
||||
var sn1 = helper.getNode("sn1");
|
||||
var sn2 = helper.getNode("sn2");
|
||||
sn2.on("input", function(msg) {
|
||||
msg.should.have.property("parts");
|
||||
msg.parts.should.have.property("count",4);
|
||||
msg.parts.should.have.property("type","array");
|
||||
msg.parts.should.have.property("index");
|
||||
if (msg.parts.index === 0) { msg.payload.should.equal(1); }
|
||||
if (msg.parts.index === 1) { msg.payload.should.equal(2); }
|
||||
if (msg.parts.index === 2) { msg.payload.should.equal(3); }
|
||||
if (msg.parts.index === 3) { msg.payload.should.equal(4); done(); }
|
||||
});
|
||||
sn1.receive({payload:[1,2,3,4]});
|
||||
});
|
||||
});
|
||||
|
||||
it('should split a string into new-lines', function(done) {
|
||||
var flow = [{id:"sn1", type:"split", wires:[["sn2"]]},
|
||||
{id:"sn2", type:"helper"}];
|
||||
helper.load(splitNode, flow, function() {
|
||||
var sn1 = helper.getNode("sn1");
|
||||
var sn2 = helper.getNode("sn2");
|
||||
sn2.on("input", function(msg) {
|
||||
msg.should.have.property("parts");
|
||||
msg.parts.should.have.property("count",4);
|
||||
msg.parts.should.have.property("type","string");
|
||||
msg.parts.should.have.property("index");
|
||||
if (msg.parts.index === 0) { msg.payload.should.equal("D"); }
|
||||
if (msg.parts.index === 1) { msg.payload.should.equal("a"); }
|
||||
if (msg.parts.index === 2) { msg.payload.should.equal("v"); }
|
||||
if (msg.parts.index === 3) { msg.payload.should.equal("e"); done(); }
|
||||
});
|
||||
sn1.receive({payload:"D\na\nv\ne"});
|
||||
});
|
||||
});
|
||||
|
||||
it('should split a string on a specified char', function(done) {
|
||||
var flow = [{id:"sn1", type:"split", wires:[["sn2"]], splt:"\n"},
|
||||
{id:"sn2", type:"helper"}];
|
||||
helper.load(splitNode, flow, function() {
|
||||
var sn1 = helper.getNode("sn1");
|
||||
var sn2 = helper.getNode("sn2");
|
||||
sn2.on("input", function(msg) {
|
||||
msg.should.have.property("parts");
|
||||
msg.parts.should.have.property("count",3);
|
||||
msg.parts.should.have.property("ch","\n");
|
||||
msg.parts.should.have.property("index");
|
||||
msg.parts.should.have.property("type","string");
|
||||
if (msg.parts.index === 0) { msg.payload.should.equal("1"); }
|
||||
if (msg.parts.index === 1) { msg.payload.should.equal("2"); }
|
||||
if (msg.parts.index === 2) { msg.payload.should.equal("3"); done(); }
|
||||
});
|
||||
sn1.receive({payload:"1\n2\n3"});
|
||||
});
|
||||
});
|
||||
|
||||
it('should split an object into pieces', function(done) {
|
||||
var flow = [{id:"sn1", type:"split", wires:[["sn2"]]},
|
||||
{id:"sn2", type:"helper"}];
|
||||
helper.load(splitNode, flow, function() {
|
||||
var sn1 = helper.getNode("sn1");
|
||||
var sn2 = helper.getNode("sn2");
|
||||
var count = 0;
|
||||
sn2.on("input", function(msg) {
|
||||
msg.should.have.property("payload");
|
||||
msg.should.have.property("parts");
|
||||
msg.parts.should.have.property("type","object");
|
||||
msg.parts.should.have.property("key");
|
||||
msg.parts.should.have.property("count");
|
||||
msg.parts.should.have.property("index");
|
||||
if (msg.parts.index === 0) { msg.payload.should.equal(1); }
|
||||
if (msg.parts.index === 1) { msg.payload.should.equal("2"); }
|
||||
if (msg.parts.index === 2) { msg.payload.should.equal(true); done(); }
|
||||
});
|
||||
sn1.receive({payload:{a:1,b:"2",c:true}});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('JOIN node', function() {
|
||||
|
||||
before(function(done) {
|
||||
helper.startServer(done);
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
helper.unload();
|
||||
});
|
||||
|
||||
it('should be loaded', function(done) {
|
||||
var flow = [{id:"joinNode1", type:"join", name:"joinNode" }];
|
||||
helper.load(joinNode, flow, function() {
|
||||
var joinNode1 = helper.getNode("joinNode1");
|
||||
joinNode1.should.have.property('name', 'joinNode');
|
||||
joinNode1.should.have.property('count', 0);
|
||||
joinNode1.should.have.property('timer', 0);
|
||||
joinNode1.should.have.property('timerr', 'send');
|
||||
joinNode1.should.have.property('build', 'array');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should join things into an array after a count', function(done) {
|
||||
var flow = [{id:"n1", type:"join", wires:[["n2"]], count:3, joiner:",",mode:"custom"},
|
||||
{id:"n2", type:"helper"}];
|
||||
helper.load(joinNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
n2.on("input", function(msg) {
|
||||
try {
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.should.be.an.Array;
|
||||
msg.payload[0].should.equal(1);
|
||||
msg.payload[1].should.equal(true);
|
||||
//msg.payload[2].a.should.equal(1);
|
||||
done();
|
||||
}
|
||||
catch(e) {done(e);}
|
||||
});
|
||||
n1.receive({payload:1});
|
||||
n1.receive({payload:true});
|
||||
n1.receive({payload:{a:1}});
|
||||
});
|
||||
});
|
||||
|
||||
it('should join things into an object after a count', function(done) {
|
||||
var flow = [{id:"n1", type:"join", wires:[["n2"]], count:5, build:"object",mode:"custom"},
|
||||
{id:"n2", type:"helper"}];
|
||||
helper.load(joinNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
n2.on("input", function(msg) {
|
||||
try {
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.should.have.property("a",1);
|
||||
msg.payload.should.have.property("b","2");
|
||||
msg.payload.should.have.property("c",true);
|
||||
msg.payload.should.have.property("d");
|
||||
msg.payload.d.should.have.property("e",7);
|
||||
msg.payload.should.have.property("g");
|
||||
msg.payload.g.should.have.property("f",6);
|
||||
done();
|
||||
}
|
||||
catch(e) { done(e)}
|
||||
});
|
||||
n1.receive({payload:1, topic:"a"});
|
||||
n1.receive({payload:"2", topic:"b"});
|
||||
n1.receive({payload:true, topic:"c"});
|
||||
n1.receive({payload:{e:5}, topic:"d"});
|
||||
n1.receive({payload:{e:7}, topic:"d"});
|
||||
n1.receive({payload:{f:6}, topic:"g"});
|
||||
});
|
||||
});
|
||||
|
||||
it('should merge objects', function(done) {
|
||||
var flow = [{id:"n1", type:"join", wires:[["n2"]], count:6, build:"merged",mode:"custom"},
|
||||
{id:"n2", type:"helper"}];
|
||||
helper.load(joinNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
n2.on("input", function(msg) {
|
||||
try {
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.should.have.property("a",6);
|
||||
msg.payload.should.have.property("b",2);
|
||||
msg.payload.should.have.property("c",3);
|
||||
msg.payload.should.have.property("d",4);
|
||||
msg.payload.should.have.property("e",5);
|
||||
done();
|
||||
}
|
||||
catch(e) { done(e)}
|
||||
});
|
||||
n1.receive({payload:{a:1}, topic:"a"});
|
||||
n1.receive({payload:{b:2}, topic:"b"});
|
||||
n1.receive({payload:{c:3}, topic:"c"});
|
||||
n1.receive({payload:{d:4}, topic:"d"});
|
||||
n1.receive({payload:{e:5}, topic:"e"});
|
||||
n1.receive({payload:{a:6}, topic:"f"});
|
||||
});
|
||||
});
|
||||
|
||||
it('should join strings with a specifed character after a timeout', function(done) {
|
||||
var flow = [{id:"n1", type:"join", wires:[["n2"]], build:"string", timeout:0.05, count:10, joiner:",",mode:"custom"},
|
||||
{id:"n2", type:"helper"}];
|
||||
helper.load(joinNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
n2.on("input", function(msg) {
|
||||
try {
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.should.equal("a,b,c");
|
||||
done();
|
||||
}
|
||||
catch(e) { done(e) }
|
||||
});
|
||||
n1.receive({payload:"a"});
|
||||
n1.receive({payload:"b"});
|
||||
n1.receive({payload:"c"});
|
||||
});
|
||||
});
|
||||
|
||||
it('should join strings with a specifed character and complete when told to', function(done) {
|
||||
var flow = [{id:"n1", type:"join", wires:[["n2"]], build:"string", timeout:5, count:100, joiner:"\n",mode:"custom"},
|
||||
{id:"n2", type:"helper"}];
|
||||
helper.load(joinNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
n2.on("input", function(msg) {
|
||||
try {
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.should.equal("Hello\nNodeRED\nWorld\n");
|
||||
done();
|
||||
}
|
||||
catch(e) { done(e) }
|
||||
});
|
||||
n1.receive({payload:"Hello"});
|
||||
n1.receive({payload:"NodeRED"});
|
||||
n1.receive({payload:"World"});
|
||||
n1.receive({payload:'', complete:true});
|
||||
});
|
||||
});
|
||||
|
||||
it('should join split things back into an array', function(done) {
|
||||
var flow = [{id:"n1", type:"join", wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
helper.load(joinNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
n2.on("input", function(msg) {
|
||||
try {
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.should.be.an.Array;
|
||||
msg.payload[0].should.equal(1);
|
||||
msg.payload[1].should.equal(2);
|
||||
msg.payload[2].should.equal(3);
|
||||
msg.payload[3].should.equal(4);
|
||||
done();
|
||||
}
|
||||
catch(e) { done(e); }
|
||||
});
|
||||
n1.receive({payload:3, parts:{index:2, count:4, id:111}});
|
||||
n1.receive({payload:2, parts:{index:1, count:4, id:111}});
|
||||
n1.receive({payload:4, parts:{index:3, count:4, id:111}});
|
||||
n1.receive({payload:1, parts:{index:0, count:4, id:111}});
|
||||
});
|
||||
});
|
||||
|
||||
it('should join split things back into an object', function(done) {
|
||||
var flow = [{id:"n1", type:"join", wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
helper.load(joinNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
n2.on("input", function(msg) {
|
||||
try {
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.should.have.property("a",1);
|
||||
msg.payload.should.have.property("b",2);
|
||||
msg.payload.should.have.property("c",3);
|
||||
msg.payload.should.have.property("d",4);
|
||||
done();
|
||||
}
|
||||
catch(e) { done(e); }
|
||||
});
|
||||
n1.receive({payload:3, parts:{index:2, count:4, id:222, key:"c", type:"object"}});
|
||||
n1.receive({payload:2, parts:{index:1, count:4, id:222, key:"b", type:"object"}});
|
||||
n1.receive({payload:4, parts:{index:3, count:4, id:222, key:"d", type:"object"}});
|
||||
n1.receive({payload:1, parts:{index:0, count:4, id:222, key:"a", type:"object"}});
|
||||
});
|
||||
});
|
||||
|
||||
it('should join split things, send when told complete', function(done) {
|
||||
var flow = [{id:"n1", type:"join", wires:[["n2"]], timeout:0.250},
|
||||
{id:"n2", type:"helper"}];
|
||||
helper.load(joinNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
n2.on("input", function(msg) {
|
||||
try {
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.should.be.an.Array;
|
||||
(msg.payload[0] === undefined).should.be.true;
|
||||
msg.payload[1].should.equal(2);
|
||||
msg.payload[2].should.equal(3);
|
||||
msg.payload[3].should.equal(4);
|
||||
done();
|
||||
}
|
||||
catch(e) { done(e); }
|
||||
});
|
||||
n1.receive({payload:3, parts:{index:2, count:4, id:444} });
|
||||
n1.receive({payload:2, parts:{index:1, count:4, id:444} });
|
||||
n1.receive({payload:4, parts:{index:3, count:4, id:444}, complete:true});
|
||||
});
|
||||
});
|
||||
|
||||
it('should join split strings back into a word', function(done) {
|
||||
var flow = [{id:"n1", type:"join", mode:"auto", wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
helper.load(joinNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
n2.on("input", function(msg) {
|
||||
try {
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.should.be.an.Array;
|
||||
msg.payload.should.equal("abcd");
|
||||
done();
|
||||
}
|
||||
catch(e) { done(e); }
|
||||
});
|
||||
n1.receive({payload:"a", parts:{type:'string',index:0, count:4, ch:"", id:555}});
|
||||
n1.receive({payload:"d", parts:{type:'string',index:3, count:4, ch:"", id:555}});
|
||||
n1.receive({payload:"c", parts:{type:'string',index:2, count:4, ch:"", id:555}});
|
||||
n1.receive({payload:"b", parts:{type:'string',index:1, count:4, ch:"", id:555}});
|
||||
});
|
||||
});
|
||||
|
||||
it('should allow chained split-split-join-join sequences', function(done) {
|
||||
var flow = [{id:"s1", type:"split",wires:[["s2"]]},
|
||||
{id:"s2", type:"split",wires:[["j1"]]},
|
||||
{id:"j1", type:"join", mode:"auto", wires:[["j2"]]},
|
||||
{id:"j2", type:"join", mode:"auto", wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
helper.load(joinNode, flow, function() {
|
||||
var s1 = helper.getNode("s1");
|
||||
var n2 = helper.getNode("n2");
|
||||
n2.on("input", function(msg) {
|
||||
try {
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.should.eql([[1,2,3],"a\nb\nc",[7,8,9]]);
|
||||
done();
|
||||
}
|
||||
catch(e) { done(e); }
|
||||
});
|
||||
s1.receive({payload:[[1,2,3],"a\nb\nc",[7,8,9]]});
|
||||
});
|
||||
})
|
||||
|
||||
|
||||
});
|
@@ -54,7 +54,7 @@ describe('CSV node', function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
n2.on("input", function(msg) {
|
||||
msg.should.have.property('payload', { a: 1, b: 2, c: 3, d: 4 });
|
||||
msg.should.have.property('payload', { a: 1, b: 2, c: 3, d: 4 });
|
||||
done();
|
||||
});
|
||||
var testString = "1,2,3,4"+String.fromCharCode(10);
|
||||
@@ -69,7 +69,7 @@ describe('CSV node', function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
n2.on("input", function(msg) {
|
||||
msg.should.have.property('payload', { a: 1, b: 2, c: 3, d: 4 });
|
||||
msg.should.have.property('payload', { a: 1, b: 2, c: 3, d: 4 });
|
||||
done();
|
||||
});
|
||||
var testString = "1,2,3,4"+String.fromCharCode(10);
|
||||
@@ -84,7 +84,7 @@ describe('CSV node', function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
n2.on("input", function(msg) {
|
||||
msg.should.have.property('payload', { col1: 1, col2: 2, col3: 3, col4: 4 });
|
||||
msg.should.have.property('payload', { col1: 1, col2: 2, col3: 3, col4: 4 });
|
||||
done();
|
||||
});
|
||||
var testString = "1,2,3,4"+String.fromCharCode(10);
|
||||
@@ -99,7 +99,7 @@ describe('CSV node', function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
n2.on("input", function(msg) {
|
||||
msg.should.have.property('payload', { a: 1, d: 4 });
|
||||
msg.should.have.property('payload', { a: 1, d: 4 });
|
||||
done();
|
||||
});
|
||||
var testString = "1,2,3,4"+String.fromCharCode(10);
|
||||
@@ -115,6 +115,7 @@ describe('CSV node', function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
n2.on("input", function(msg) {
|
||||
//console.log(msg);
|
||||
msg.should.have.property('payload', { a: 1, b: -2, c: '+3', d: 4, e: -5, f: 'ab"cd', g: 'with,a,comma' });
|
||||
done();
|
||||
});
|
||||
@@ -133,11 +134,11 @@ describe('CSV node', function() {
|
||||
n2.on("input", function(msg) {
|
||||
//console.log(msg);
|
||||
if (c === 0) {
|
||||
msg.should.have.property('payload', { w: 1, x: 2, y: 3, z: 4 });
|
||||
msg.should.have.property('payload', { w: 1, x: 2, y: 3, z: 4 });
|
||||
c += 1;
|
||||
}
|
||||
else {
|
||||
msg.should.have.property('payload', { w: 5, x: 6, y: 7, z: 8 });
|
||||
else {
|
||||
msg.should.have.property('payload', { w: 5, x: 6, y: 7, z: 8 });
|
||||
done();
|
||||
}
|
||||
});
|
||||
@@ -153,8 +154,7 @@ describe('CSV node', function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
n2.on("input", function(msg) {
|
||||
//console.log(msg);
|
||||
msg.should.have.property('payload', [ { a: 1, b: 2, c: 3, d: 4 },{ a: 5, b: -6, c: 7, d: '+8' },{ a: 9, b: 0, c: 'a', d: 'b' },{ a: 'c', b: 'd', c: 'e', d: 'f' } ]);
|
||||
msg.should.have.property('payload', [ { a: 1, b: 2, c: 3, d: 4 },{ a: 5, b: -6, c: 7, d: '+8' },{ a: 9, b: 0, c: 'a', d: 'b' },{ a: 'c', b: 'd', c: 'e', d: 'f' } ]);
|
||||
done();
|
||||
});
|
||||
var testString = "1,2,3,4\n5,-6,07,+8\n9,0,a,b\nc,d,e,f";
|
||||
|
@@ -97,7 +97,8 @@ describe('JSON node', function() {
|
||||
return evt[0].type == "json";
|
||||
});
|
||||
logEvents.should.have.length(1);
|
||||
logEvents[0][0].should.have.a.property('msg',"Unexpected token o");
|
||||
logEvents[0][0].should.have.a.property('msg');
|
||||
logEvents[0][0].msg.should.startWith("Unexpected token o");
|
||||
logEvents[0][0].should.have.a.property('level',helper.log().ERROR);
|
||||
done();
|
||||
} catch(err) {
|
||||
|
@@ -71,11 +71,17 @@ describe('file Nodes', function() {
|
||||
n1.emit("input", {payload:"test2"}); // string
|
||||
setTimeout(function() {
|
||||
n1.emit("input", {payload:true}); // boolean
|
||||
},50);
|
||||
},30);
|
||||
setTimeout(function() {
|
||||
n1.emit("input", {payload:999}); // number
|
||||
},60);
|
||||
setTimeout(function() {
|
||||
n1.emit("input", {payload:[2]}); // object (array)
|
||||
},90);
|
||||
setTimeout(function() {
|
||||
var f = fs.readFileSync(fileToTest).toString();
|
||||
f.should.have.length(11);
|
||||
f.should.equal("test2\ntrue\n");
|
||||
f.should.have.length(19);
|
||||
f.should.equal("test2\ntrue\n999\n[2]\n");
|
||||
done();
|
||||
},wait);
|
||||
});
|
||||
|
@@ -82,11 +82,11 @@ module.exports = {
|
||||
// do nothing
|
||||
},
|
||||
};
|
||||
|
||||
var settings = {
|
||||
available: function() { return false; }
|
||||
};
|
||||
|
||||
|
||||
var red = {};
|
||||
for (var i in RED) {
|
||||
if (RED.hasOwnProperty(i) && !/^(init|start|stop)$/.test(i)) {
|
||||
@@ -115,6 +115,7 @@ module.exports = {
|
||||
cb();
|
||||
});
|
||||
},
|
||||
|
||||
unload: function() {
|
||||
// TODO: any other state to remove between tests?
|
||||
redNodes.clearRegistry();
|
||||
@@ -137,7 +138,7 @@ module.exports = {
|
||||
},
|
||||
|
||||
startServer: function(done) {
|
||||
server = http.createServer(function(req,res){app(req,res);});
|
||||
server = http.createServer(function(req,res) { app(req,res); });
|
||||
RED.init(server, {
|
||||
SKIP_BUILD_CHECK: true,
|
||||
logging:{console:{level:'off'}}
|
||||
@@ -150,9 +151,10 @@ module.exports = {
|
||||
done();
|
||||
});
|
||||
},
|
||||
|
||||
//TODO consider saving TCP handshake/server reinit on start/stop/start sequences
|
||||
stopServer: function(done) {
|
||||
if(server) {
|
||||
if (server) {
|
||||
try {
|
||||
server.close(done);
|
||||
} catch(e) {
|
||||
|
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* Copyright 2015 IBM Corp.
|
||||
* Copyright 2015, 2016 IBM Corp.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -31,14 +31,22 @@ describe("Auth permissions", function() {
|
||||
permissions.hasPermission(["read"],"node.read").should.be.true;
|
||||
permissions.hasPermission(["read"],"write").should.be.false;
|
||||
permissions.hasPermission(["read"],"node.write").should.be.false;
|
||||
permissions.hasPermission(["*.read"],"read").should.be.true;
|
||||
permissions.hasPermission(["*.read"],"node.read").should.be.true;
|
||||
permissions.hasPermission(["*.read"],"write").should.be.false;
|
||||
permissions.hasPermission(["*.read"],"node.write").should.be.false;
|
||||
});
|
||||
it('a user with foo permissions',function() {
|
||||
permissions.hasPermission("foo","foo").should.be.false;
|
||||
permissions.hasPermission("foo","foo").should.be.true;
|
||||
});
|
||||
it('an array of permissions', function() {
|
||||
permissions.hasPermission(["*"],["foo.read","foo.write"]).should.be.true;
|
||||
permissions.hasPermission("read",["foo.read","foo.write"]).should.be.false;
|
||||
permissions.hasPermission("read",["foo.read","bar.read"]).should.be.true;
|
||||
permissions.hasPermission(["flows.read"],["flows.read"]).should.be.true;
|
||||
permissions.hasPermission(["flows.read"],["flows.write"]).should.be.false;
|
||||
permissions.hasPermission(["flows.read","nodes.write"],["flows.write"]).should.be.false;
|
||||
permissions.hasPermission(["flows.read","nodes.write"],["nodes.write"]).should.be.true;
|
||||
});
|
||||
it('permits an empty permission', function() {
|
||||
permissions.hasPermission("*","").should.be.true;
|
||||
|
@@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
**/
|
||||
|
||||
|
||||
var should = require("should");
|
||||
var when = require("when");
|
||||
var sinon = require("sinon");
|
||||
@@ -24,16 +24,11 @@ var Tokens = require("../../../../red/api/auth/tokens");
|
||||
describe("Tokens", function() {
|
||||
describe("#init",function() {
|
||||
it('loads sessions', function(done) {
|
||||
Tokens.init({},{
|
||||
getSessions:function() {
|
||||
done();
|
||||
return when.resolve();
|
||||
}
|
||||
});
|
||||
Tokens.init({}).then(done);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
describe("#get",function() {
|
||||
it('returns a valid token', function(done) {
|
||||
Tokens.init({},{
|
||||
@@ -51,7 +46,7 @@ describe("Tokens", function() {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
it('returns null for an invalid token', function(done) {
|
||||
Tokens.init({},{
|
||||
getSessions:function() {
|
||||
@@ -98,7 +93,7 @@ describe("Tokens", function() {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe("#create",function() {
|
||||
it('creates a token', function(done) {
|
||||
var savedSession;
|
||||
@@ -112,14 +107,14 @@ describe("Tokens", function() {
|
||||
}
|
||||
});
|
||||
var expectedExpiryTime = Date.now()+10000;
|
||||
|
||||
|
||||
|
||||
|
||||
Tokens.create("user","client","scope").then(function(token) {
|
||||
try {
|
||||
should.exist(savedSession);
|
||||
var sessionKeys = Object.keys(savedSession);
|
||||
sessionKeys.should.have.lengthOf(1);
|
||||
|
||||
|
||||
token.should.have.a.property('accessToken',sessionKeys[0]);
|
||||
savedSession[sessionKeys[0]].should.have.a.property('user','user');
|
||||
savedSession[sessionKeys[0]].should.have.a.property('client','client');
|
||||
@@ -133,7 +128,7 @@ describe("Tokens", function() {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe("#revoke", function() {
|
||||
it('revokes a token', function(done) {
|
||||
var savedSession;
|
||||
@@ -157,5 +152,5 @@ describe("Tokens", function() {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
@@ -50,7 +50,8 @@ describe("flows api", function() {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.be.an.Array.and.have.lengthOf(3);
|
||||
res.body.should.be.an.Array;
|
||||
res.body.should.have.lengthOf(3);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
@@ -15,5 +15,5 @@
|
||||
**/
|
||||
|
||||
describe("locales api", function() {
|
||||
it.skip("works",function(){});
|
||||
it.skip("works",function() {});
|
||||
});
|
||||
|
@@ -44,10 +44,10 @@ describe("nodes api", function() {
|
||||
app.use(bodyParser.json());
|
||||
app.get("/nodes",nodes.getAll);
|
||||
app.post("/nodes",nodes.post);
|
||||
app.get("/nodes/:mod",nodes.getModule);
|
||||
app.get("/nodes/:mod/:set",nodes.getSet);
|
||||
app.put("/nodes/:mod",nodes.putModule);
|
||||
app.put("/nodes/:mod/:set",nodes.putSet);
|
||||
app.get(/\/nodes\/((@[^\/]+\/)?[^\/]+)$/,nodes.getModule);
|
||||
app.get(/\/nodes\/((@[^\/]+\/)?[^\/]+)\/([^\/]+)$/,nodes.getSet);
|
||||
app.put(/\/nodes\/((@[^\/]+\/)?[^\/]+)$/,nodes.putModule);
|
||||
app.put(/\/nodes\/((@[^\/]+\/)?[^\/]+)\/([^\/]+)$/,nodes.putSet);
|
||||
app.delete("/nodes/:id",nodes.delete);
|
||||
sinon.stub(comms,"publish");
|
||||
sinon.stub(locales,"determineLangFromHeaders", function() {
|
||||
@@ -76,7 +76,8 @@ describe("nodes api", function() {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
res.body.should.be.an.Array.and.have.lengthOf(3);
|
||||
res.body.should.be.an.Array;
|
||||
res.body.should.have.lengthOf(3);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
@@ -108,4 +108,15 @@ describe('context', function() {
|
||||
context2.global.get("foo").should.eql("test");
|
||||
});
|
||||
|
||||
it('deletes context',function() {
|
||||
var context = Context.get("1","flowA");
|
||||
should.not.exist(context.get("foo"));
|
||||
context.set("foo","abc");
|
||||
context.get("foo").should.eql("abc");
|
||||
|
||||
Context.delete("1","flowA");
|
||||
context = Context.get("1","flowA");
|
||||
should.not.exist(context.get("foo"));
|
||||
})
|
||||
|
||||
});
|
||||
|
@@ -45,7 +45,7 @@ describe('Flow', function() {
|
||||
|
||||
var TestNode = function(n) {
|
||||
Node.call(this,n);
|
||||
createCount++;
|
||||
this._index = createCount++;
|
||||
this.scope = n.scope;
|
||||
var node = this;
|
||||
this.foo = n.foo;
|
||||
@@ -173,6 +173,61 @@ describe('Flow', function() {
|
||||
});
|
||||
|
||||
|
||||
it("instantiates config nodes in the right order",function(done) {
|
||||
var config = flowUtils.parseConfig([
|
||||
{id:"t1",type:"tab"},
|
||||
{id:"1",x:10,y:10,z:"t1",type:"test",foo:"a",wires:["2"]},
|
||||
{id:"2",x:10,y:10,z:"t1",type:"test",foo:"a",wires:["3"]},
|
||||
{id:"3",x:10,y:10,z:"t1",type:"test",foo:"a",wires:[]},
|
||||
{id:"4",z:"t1",type:"test",foo:"5"}, // This node depends on #5
|
||||
{id:"5",z:"t1",type:"test"}
|
||||
]);
|
||||
var flow = Flow.create(config,config.flows["t1"]);
|
||||
flow.start();
|
||||
|
||||
Object.keys(flow.getActiveNodes()).should.have.length(5);
|
||||
|
||||
|
||||
currentNodes.should.have.a.property("1");
|
||||
currentNodes.should.have.a.property("2");
|
||||
currentNodes.should.have.a.property("3");
|
||||
currentNodes.should.have.a.property("4");
|
||||
currentNodes.should.have.a.property("5");
|
||||
|
||||
currentNodes["1"].should.have.a.property("_index",2);
|
||||
currentNodes["2"].should.have.a.property("_index",3);
|
||||
currentNodes["3"].should.have.a.property("_index",4);
|
||||
currentNodes["4"].should.have.a.property("_index",1);
|
||||
currentNodes["5"].should.have.a.property("_index",0);
|
||||
|
||||
flow.stop().then(function() {
|
||||
currentNodes.should.not.have.a.property("1");
|
||||
currentNodes.should.not.have.a.property("2");
|
||||
currentNodes.should.not.have.a.property("3");
|
||||
currentNodes.should.not.have.a.property("4");
|
||||
currentNodes.should.not.have.a.property("5");
|
||||
stoppedNodes.should.have.a.property("1");
|
||||
stoppedNodes.should.have.a.property("2");
|
||||
stoppedNodes.should.have.a.property("3");
|
||||
stoppedNodes.should.have.a.property("4");
|
||||
stoppedNodes.should.have.a.property("5");
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
it("detects dependency loops in config nodes",function() {
|
||||
var config = flowUtils.parseConfig([
|
||||
{id:"t1",type:"tab"},
|
||||
{id:"node1",z:"t1",type:"test",foo:"node2"}, // This node depends on #5
|
||||
{id:"node2",z:"t1",type:"test",foo:"node1"}
|
||||
]);
|
||||
var flow = Flow.create(config,config.flows["t1"]);
|
||||
/*jshint immed: false */
|
||||
(function(){
|
||||
flow.start();
|
||||
}).should.throw("Circular config node dependency detected: node1");
|
||||
});
|
||||
it("instantiates a subflow and stops it",function(done) {
|
||||
var config = flowUtils.parseConfig([
|
||||
{id:"t1",type:"tab"},
|
||||
|
@@ -38,42 +38,42 @@ describe('flows/util', function() {
|
||||
|
||||
describe('#diffNodes',function() {
|
||||
it('handles a null old node', function() {
|
||||
flowUtil.diffNodes(null,{}).should.be.true;
|
||||
flowUtil.diffNodes(null,{}).should.be.true();
|
||||
});
|
||||
it('ignores x/y changes', function() {
|
||||
flowUtil.diffNodes({x:10,y:10},{x:20,y:10}).should.be.false;
|
||||
flowUtil.diffNodes({x:10,y:10},{x:10,y:20}).should.be.false;
|
||||
flowUtil.diffNodes({x:10,y:10},{x:20,y:10}).should.be.false();
|
||||
flowUtil.diffNodes({x:10,y:10},{x:10,y:20}).should.be.false();
|
||||
});
|
||||
it('ignores wiring changes', function() {
|
||||
flowUtil.diffNodes({wires:[]},{wires:[1,2,3]}).should.be.false;
|
||||
flowUtil.diffNodes({wires:[]},{wires:[1,2,3]}).should.be.false();
|
||||
});
|
||||
it('spots existing property change - string', function() {
|
||||
flowUtil.diffNodes({a:"foo"},{a:"bar"}).should.be.true;
|
||||
flowUtil.diffNodes({a:"foo"},{a:"bar"}).should.be.true();
|
||||
});
|
||||
it('spots existing property change - number', function() {
|
||||
flowUtil.diffNodes({a:0},{a:1}).should.be.true;
|
||||
flowUtil.diffNodes({a:0},{a:1}).should.be.true();
|
||||
});
|
||||
it('spots existing property change - boolean', function() {
|
||||
flowUtil.diffNodes({a:true},{a:false}).should.be.true;
|
||||
flowUtil.diffNodes({a:true},{a:false}).should.be.true();
|
||||
});
|
||||
it('spots existing property change - truthy', function() {
|
||||
flowUtil.diffNodes({a:true},{a:1}).should.be.true;
|
||||
flowUtil.diffNodes({a:true},{a:1}).should.be.true();
|
||||
});
|
||||
it('spots existing property change - falsey', function() {
|
||||
flowUtil.diffNodes({a:false},{a:0}).should.be.true;
|
||||
flowUtil.diffNodes({a:false},{a:0}).should.be.true();
|
||||
});
|
||||
it('spots existing property change - array', function() {
|
||||
flowUtil.diffNodes({a:[0,1,2]},{a:[0,2,3]}).should.be.true;
|
||||
flowUtil.diffNodes({a:[0,1,2]},{a:[0,2,3]}).should.be.true();
|
||||
});
|
||||
it('spots existing property change - object', function() {
|
||||
flowUtil.diffNodes({a:{a:[0,1,2]}},{a:{a:[0,2,3]}}).should.be.true;
|
||||
flowUtil.diffNodes({a:{a:[0,1,2]}},{a:{b:[0,1,2]}}).should.be.true;
|
||||
flowUtil.diffNodes({a:{a:[0,1,2]}},{a:{a:[0,2,3]}}).should.be.true();
|
||||
flowUtil.diffNodes({a:{a:[0,1,2]}},{a:{b:[0,1,2]}}).should.be.true();
|
||||
});
|
||||
it('spots added property', function() {
|
||||
flowUtil.diffNodes({a:"foo"},{a:"foo",b:"bar"}).should.be.true;
|
||||
flowUtil.diffNodes({a:"foo"},{a:"foo",b:"bar"}).should.be.true();
|
||||
});
|
||||
it('spots removed property', function() {
|
||||
flowUtil.diffNodes({a:"foo",b:"bar"},{a:"foo"}).should.be.true;
|
||||
flowUtil.diffNodes({a:"foo",b:"bar"},{a:"foo"}).should.be.true();
|
||||
});
|
||||
|
||||
|
||||
@@ -88,8 +88,7 @@ describe('flows/util', function() {
|
||||
];
|
||||
var parsedConfig = flowUtil.parseConfig(originalConfig);
|
||||
var expectedConfig = {"allNodes":{"t1-1":{"id":"t1-1","x":10,"y":10,"z":"t1","type":"test","wires":[]},"t1":{"id":"t1","type":"tab"}},"subflows":{},"configs":{},"flows":{"t1":{"id":"t1","type":"tab","subflows":{},"configs":{},"nodes":{"t1-1":{"id":"t1-1","x":10,"y":10,"z":"t1","type":"test","wires":[]}}}},"missingTypes":[]};
|
||||
|
||||
redUtil.compareObjects(parsedConfig,expectedConfig).should.be.true;
|
||||
parsedConfig.should.eql(expectedConfig);
|
||||
});
|
||||
|
||||
it('parses a single-tab flow with global config node', function() {
|
||||
@@ -100,7 +99,7 @@ describe('flows/util', function() {
|
||||
];
|
||||
var parsedConfig = flowUtil.parseConfig(originalConfig);
|
||||
var expectedConfig = {"allNodes":{"t1-1":{"id":"t1-1","x":10,"y":10,"z":"t1","type":"test","foo":"cn","wires":[]},"cn":{"id":"cn","type":"test"},"t1":{"id":"t1","type":"tab"}},"subflows":{},"configs":{"cn":{"id":"cn","type":"test","_users":["t1-1"]}},"flows":{"t1":{"id":"t1","type":"tab","subflows":{},"configs":{},"nodes":{"t1-1":{"id":"t1-1","x":10,"y":10,"z":"t1","type":"test","foo":"cn","wires":[]}}}},"missingTypes":[]};
|
||||
redUtil.compareObjects(parsedConfig,expectedConfig).should.be.true;
|
||||
parsedConfig.should.eql(expectedConfig);
|
||||
});
|
||||
|
||||
it('parses a multi-tab flow', function() {
|
||||
@@ -112,8 +111,7 @@ describe('flows/util', function() {
|
||||
];
|
||||
var parsedConfig = flowUtil.parseConfig(originalConfig);
|
||||
var expectedConfig = {"allNodes":{"t1":{"id":"t1","type":"tab"},"t1-1":{"id":"t1-1","x":10,"y":10,"z":"t1","type":"test","wires":[]},"t2":{"id":"t2","type":"tab"},"t2-1":{"id":"t2-1","x":10,"y":10,"z":"t2","type":"test","wires":[]}},"subflows":{},"configs":{},"flows":{"t1":{"id":"t1","type":"tab","subflows":{},"configs":{},"nodes":{"t1-1":{"id":"t1-1","x":10,"y":10,"z":"t1","type":"test","wires":[]}}},"t2":{"id":"t2","type":"tab","subflows":{},"configs":{},"nodes":{"t2-1":{"id":"t2-1","x":10,"y":10,"z":"t2","type":"test","wires":[]}}}},"missingTypes":[]};
|
||||
|
||||
redUtil.compareObjects(parsedConfig,expectedConfig).should.be.true;
|
||||
parsedConfig.should.eql(expectedConfig);
|
||||
});
|
||||
|
||||
it('parses a subflow flow', function() {
|
||||
@@ -125,8 +123,7 @@ describe('flows/util', function() {
|
||||
];
|
||||
var parsedConfig = flowUtil.parseConfig(originalConfig);
|
||||
var expectedConfig = {"allNodes":{"t1":{"id":"t1","type":"tab"},"t1-1":{"id":"t1-1","x":10,"y":10,"z":"t1","type":"subflow:sf1","wires":[]},"sf1":{"id":"sf1","type":"subflow"},"sf1-1":{"id":"sf1-1","x":10,"y":10,"z":"sf1","type":"test","wires":[]}},"subflows":{"sf1":{"id":"sf1","type":"subflow","configs":{},"nodes":{"sf1-1":{"id":"sf1-1","x":10,"y":10,"z":"sf1","type":"test","wires":[]}},"instances":[{"id":"t1-1","x":10,"y":10,"z":"t1","type":"subflow:sf1","wires":[],"subflow":"sf1"}]}},"configs":{},"flows":{"t1":{"id":"t1","type":"tab","subflows":{},"configs":{},"nodes":{"t1-1":{"id":"t1-1","x":10,"y":10,"z":"t1","type":"subflow:sf1","wires":[],"subflow":"sf1"}}}},"missingTypes":[]};
|
||||
|
||||
redUtil.compareObjects(parsedConfig,expectedConfig).should.be.true;
|
||||
parsedConfig.should.eql(expectedConfig);
|
||||
});
|
||||
|
||||
it('parses a flow with a missing type', function() {
|
||||
@@ -138,7 +135,17 @@ describe('flows/util', function() {
|
||||
var parsedConfig = flowUtil.parseConfig(originalConfig);
|
||||
parsedConfig.missingTypes.should.eql(['missing']);
|
||||
var expectedConfig = {"allNodes":{"t1":{"id":"t1","type":"tab"},"t1-1":{"id":"t1-1","x":10,"y":10,"z":"t1","type":"sf1","wires":[]},"t1-2":{"id":"t1-2","x":10,"y":10,"z":"t1","type":"missing","wires":[]}},"subflows":{},"configs":{},"flows":{"t1":{"id":"t1","type":"tab","subflows":{},"configs":{},"nodes":{"t1-1":{"id":"t1-1","x":10,"y":10,"z":"t1","type":"sf1","wires":[]},'t1-2': { id: 't1-2', x: 10, y: 10, z: 't1', type: 'missing', wires: [] }}}},"missingTypes":["missing"]};
|
||||
redUtil.compareObjects(parsedConfig,expectedConfig).should.be.true;
|
||||
redUtil.compareObjects(parsedConfig,expectedConfig).should.be.true();
|
||||
});
|
||||
|
||||
it('parses a flow with a missing flow', function() {
|
||||
var originalConfig = [
|
||||
{id:"t1-1",x:10,y:10,z:"t1",type:"test",foo:"cn", wires:[]},
|
||||
{id:"cn",type:"test"},
|
||||
];
|
||||
var parsedConfig = flowUtil.parseConfig(originalConfig);
|
||||
var expectedConfig = {"allNodes":{"t1-1":{"id":"t1-1","x":10,"y":10,"z":"t1","type":"test","foo":"cn","wires":[]},"cn":{"id":"cn","type":"test"}},"subflows":{},"configs":{"cn":{"id":"cn","type":"test","_users":["t1-1"]}},"flows":{"t1":{"id":"t1","type":"tab","subflows":{},"configs":{},"nodes":{"t1-1":{"id":"t1-1","x":10,"y":10,"z":"t1","type":"test","foo":"cn","wires":[]}}}},"missingTypes":[]};
|
||||
parsedConfig.should.eql(expectedConfig);
|
||||
});
|
||||
|
||||
|
||||
@@ -308,7 +315,7 @@ describe('flows/util', function() {
|
||||
diffResult.linked.sort().should.eql(["3"]);
|
||||
});
|
||||
|
||||
it('identifies config nodes changes', function() {
|
||||
it('identifies config nodes changes, node->config', function() {
|
||||
var config = [
|
||||
{id:"1",type:"test",foo:"configNode",wires:[["2"]]},
|
||||
{id:"2",type:"test",bar:"b",wires:[["3"]]},
|
||||
@@ -329,7 +336,30 @@ describe('flows/util', function() {
|
||||
diffResult.removed.should.have.length(0);
|
||||
diffResult.rewired.should.have.length(0);
|
||||
diffResult.linked.sort().should.eql(["2","3"]);
|
||||
});
|
||||
|
||||
it('identifies config nodes changes, node->config->config', function() {
|
||||
var config = [
|
||||
{id:"1",type:"test",foo:"configNode1",wires:[["2"]]},
|
||||
{id:"2",type:"test",bar:"b",wires:[["3"]]},
|
||||
{id:"3",type:"test",foo:"a",wires:[]},
|
||||
{id:"configNode1",foo:"configNode2",type:"testConfig"},
|
||||
{id:"configNode2",type:"testConfig"}
|
||||
];
|
||||
var newConfig = clone(config);
|
||||
newConfig[4].foo = "bar";
|
||||
|
||||
var originalConfig = flowUtil.parseConfig(config);
|
||||
var changedConfig = flowUtil.parseConfig(newConfig);
|
||||
|
||||
originalConfig.missingTypes.should.have.length(0);
|
||||
|
||||
var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);
|
||||
diffResult.added.should.have.length(0);
|
||||
diffResult.changed.sort().should.eql(["1","configNode1","configNode2"]);
|
||||
diffResult.removed.should.have.length(0);
|
||||
diffResult.rewired.should.have.length(0);
|
||||
diffResult.linked.sort().should.eql(["2","3"]);
|
||||
});
|
||||
|
||||
it('marks a parent subflow as changed for an internal property change', function() {
|
||||
|
@@ -114,7 +114,8 @@ describe("red/nodes/registry/loader",function() {
|
||||
registry.addNodeSet.lastCall.args[1].should.not.have.a.property('err');
|
||||
|
||||
nodes.registerType.calledOnce.should.be.true;
|
||||
nodes.registerType.lastCall.args[0].should.eql('test-node-1');
|
||||
nodes.registerType.lastCall.args[0].should.eql('node-red/TestNode1');
|
||||
nodes.registerType.lastCall.args[1].should.eql('test-node-1');
|
||||
|
||||
done();
|
||||
}).otherwise(function(err) {
|
||||
@@ -162,8 +163,10 @@ describe("red/nodes/registry/loader",function() {
|
||||
registry.addNodeSet.lastCall.args[1].should.not.have.a.property('err');
|
||||
|
||||
nodes.registerType.calledTwice.should.be.true;
|
||||
nodes.registerType.firstCall.args[0].should.eql('test-node-multiple-1a');
|
||||
nodes.registerType.secondCall.args[0].should.eql('test-node-multiple-1b');
|
||||
nodes.registerType.firstCall.args[0].should.eql('node-red/MultipleNodes1');
|
||||
nodes.registerType.firstCall.args[1].should.eql('test-node-multiple-1a');
|
||||
nodes.registerType.secondCall.args[0].should.eql('node-red/MultipleNodes1');
|
||||
nodes.registerType.secondCall.args[1].should.eql('test-node-multiple-1b');
|
||||
|
||||
done();
|
||||
}).otherwise(function(err) {
|
||||
@@ -212,7 +215,8 @@ describe("red/nodes/registry/loader",function() {
|
||||
registry.addNodeSet.lastCall.args[1].should.not.have.a.property('err');
|
||||
|
||||
nodes.registerType.calledOnce.should.be.true;
|
||||
nodes.registerType.lastCall.args[0].should.eql('test-node-2');
|
||||
nodes.registerType.lastCall.args[0].should.eql('node-red/TestNode2');
|
||||
nodes.registerType.lastCall.args[1].should.eql('test-node-2');
|
||||
|
||||
done();
|
||||
}).otherwise(function(err) {
|
||||
|
@@ -20,8 +20,12 @@ var sinon = require("sinon");
|
||||
|
||||
var typeRegistry = require("../../../../../red/runtime/nodes/registry/registry");
|
||||
|
||||
var Node = require("../../../../../red/runtime/nodes/Node");
|
||||
|
||||
var events = require("../../../../../red/runtime/events");
|
||||
|
||||
var inherits = require("util").inherits;
|
||||
|
||||
describe("red/nodes/registry/registry",function() {
|
||||
|
||||
afterEach(function() {
|
||||
@@ -428,31 +432,52 @@ describe("red/nodes/registry/registry",function() {
|
||||
});
|
||||
|
||||
describe('#registerNodeConstructor', function() {
|
||||
function TestNodeConstructor() {
|
||||
}
|
||||
var TestNodeConstructor;
|
||||
beforeEach(function() {
|
||||
TestNodeConstructor = function TestNodeConstructor() {
|
||||
};
|
||||
sinon.stub(events,'emit');
|
||||
});
|
||||
afterEach(function() {
|
||||
events.emit.restore();
|
||||
});
|
||||
it('registers a node constructor', function() {
|
||||
typeRegistry.registerNodeConstructor('node-type',TestNodeConstructor);
|
||||
typeRegistry.registerNodeConstructor('node-set','node-type',TestNodeConstructor);
|
||||
events.emit.calledOnce.should.be.true;
|
||||
events.emit.lastCall.args[0].should.eql('type-registered');
|
||||
events.emit.lastCall.args[1].should.eql('node-type');
|
||||
})
|
||||
it('throws error on duplicate node registration', function() {
|
||||
typeRegistry.registerNodeConstructor('node-type',TestNodeConstructor);
|
||||
typeRegistry.registerNodeConstructor('node-set','node-type',TestNodeConstructor);
|
||||
events.emit.calledOnce.should.be.true;
|
||||
events.emit.lastCall.args[0].should.eql('type-registered');
|
||||
events.emit.lastCall.args[1].should.eql('node-type');
|
||||
/*jshint immed: false */
|
||||
(function(){
|
||||
typeRegistry.registerNodeConstructor('node-type',TestNodeConstructor);
|
||||
typeRegistry.registerNodeConstructor('node-set','node-type',TestNodeConstructor);
|
||||
}).should.throw("node-type already registered");
|
||||
events.emit.calledOnce.should.be.true;
|
||||
})
|
||||
});
|
||||
it('extends a constructor with the Node constructor', function() {
|
||||
TestNodeConstructor.prototype.should.not.be.an.instanceOf(Node);
|
||||
typeRegistry.registerNodeConstructor('node-set','node-type',TestNodeConstructor);
|
||||
TestNodeConstructor.prototype.should.be.an.instanceOf(Node);
|
||||
});
|
||||
it('does not override a constructor\'s prototype', function() {
|
||||
function Foo(){};
|
||||
inherits(TestNodeConstructor,Foo);
|
||||
TestNodeConstructor.prototype.should.be.an.instanceOf(Foo);
|
||||
TestNodeConstructor.prototype.should.not.be.an.instanceOf(Node);
|
||||
|
||||
typeRegistry.registerNodeConstructor('node-set','node-type',TestNodeConstructor);
|
||||
|
||||
TestNodeConstructor.prototype.should.be.an.instanceOf(Node);
|
||||
TestNodeConstructor.prototype.should.be.an.instanceOf(Foo);
|
||||
|
||||
typeRegistry.registerNodeConstructor('node-set','node-type2',TestNodeConstructor);
|
||||
TestNodeConstructor.prototype.should.be.an.instanceOf(Node);
|
||||
TestNodeConstructor.prototype.should.be.an.instanceOf(Foo);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
@@ -37,11 +37,13 @@ describe("red/settings", function() {
|
||||
|
||||
settings.a.should.equal(123);
|
||||
settings.b.should.equal("test");
|
||||
settings.c.should.be.an.Array.with.lengthOf(3);
|
||||
settings.c.should.be.an.Array;
|
||||
settings.c.should.have.lengthOf(3);
|
||||
|
||||
settings.get("a").should.equal(123);
|
||||
settings.get("b").should.equal("test");
|
||||
settings.get("c").should.be.an.Array.with.lengthOf(3);
|
||||
settings.get("c").should.be.an.Array;
|
||||
settings.get("c").should.have.lengthOf(3);
|
||||
|
||||
/*jshint immed: false */
|
||||
(function() {
|
||||
@@ -49,7 +51,8 @@ describe("red/settings", function() {
|
||||
}).should.throw();
|
||||
|
||||
settings.c.push(5);
|
||||
settings.c.should.be.an.Array.with.lengthOf(4);
|
||||
settings.c.should.be.an.Array;
|
||||
settings.c.should.have.lengthOf(4);
|
||||
|
||||
/*jshint immed: false */
|
||||
(function() {
|
||||
@@ -129,7 +132,8 @@ describe("red/settings", function() {
|
||||
|
||||
settings.should.have.property("a",123);
|
||||
settings.should.have.property("b","test");
|
||||
settings.c.should.be.an.Array.with.lengthOf(3);
|
||||
settings.c.should.be.an.Array;
|
||||
settings.c.should.have.lengthOf(3);
|
||||
|
||||
settings.reset();
|
||||
|
||||
|
@@ -58,6 +58,7 @@ describe("red/util", function() {
|
||||
it('Buffer', function() {
|
||||
util.compareObjects(new Buffer("hello"),new Buffer("hello")).should.equal(true);
|
||||
util.compareObjects(new Buffer("hello"),new Buffer("hello ")).should.equal(false);
|
||||
util.compareObjects(new Buffer("hello"),"hello").should.equal(false);
|
||||
});
|
||||
|
||||
});
|
||||
@@ -157,7 +158,16 @@ describe("red/util", function() {
|
||||
(function() {
|
||||
util.getMessageProperty({a:"foo"},"msg.a.b.c");
|
||||
}).should.throw();
|
||||
})
|
||||
});
|
||||
it('retrieves a property with array syntax', function() {
|
||||
var v = util.getMessageProperty({a:["foo","bar"]},"msg.a[0]");
|
||||
v.should.eql("foo");
|
||||
var v2 = util.getMessageProperty({a:[null,{b:"foo"}]},"a[1].b");
|
||||
v2.should.eql("foo");
|
||||
var v3 = util.getMessageProperty({a:[[["foo"]]]},"a[0][0][0]");
|
||||
v3.should.eql("foo");
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('setMessageProperty', function() {
|
||||
@@ -190,7 +200,48 @@ describe("red/util", function() {
|
||||
var msg = {a:{}};
|
||||
util.setMessageProperty(msg,"msg.a.b.c",undefined);
|
||||
should.not.exist(msg.a.b);
|
||||
});
|
||||
it('sets a property with array syntax', function() {
|
||||
var msg = {a:{b:["foo",{c:["",""]}]}};
|
||||
util.setMessageProperty(msg,"msg.a.b[1].c[1]","bar");
|
||||
msg.a.b[1].c[1].should.eql('bar');
|
||||
});
|
||||
it('creates missing array elements - final property', function() {
|
||||
var msg = {a:[]};
|
||||
util.setMessageProperty(msg,"msg.a[2]","bar");
|
||||
msg.a.should.have.length(3);
|
||||
msg.a[2].should.eql("bar");
|
||||
});
|
||||
it('creates missing array elements - mid property', function() {
|
||||
var msg = {};
|
||||
util.setMessageProperty(msg,"msg.a[2].b","bar");
|
||||
msg.a.should.have.length(3);
|
||||
msg.a[2].b.should.eql("bar");
|
||||
});
|
||||
it('creates missing array elements - multi-arrays', function() {
|
||||
var msg = {};
|
||||
util.setMessageProperty(msg,"msg.a[2][2]","bar");
|
||||
msg.a.should.have.length(3);
|
||||
msg.a.should.be.instanceOf(Array);
|
||||
msg.a[2].should.have.length(3);
|
||||
msg.a[2].should.be.instanceOf(Array);
|
||||
msg.a[2][2].should.eql("bar");
|
||||
});
|
||||
it('does not create missing array elements - final property', function() {
|
||||
var msg = {a:{}};
|
||||
util.setMessageProperty(msg,"msg.a.b[2]","bar",false);
|
||||
should.not.exist(msg.a.b);
|
||||
// check it has not been misinterpreted
|
||||
msg.a.should.not.have.property("b[2]");
|
||||
});
|
||||
it('deletes property inside array if value is undefined', function() {
|
||||
var msg = {a:[1,2,3]};
|
||||
util.setMessageProperty(msg,"msg.a[1]",undefined);
|
||||
msg.a.should.have.length(2);
|
||||
msg.a[0].should.eql(1);
|
||||
msg.a[1].should.eql(3);
|
||||
})
|
||||
|
||||
});
|
||||
|
||||
describe('evaluateNodeProperty', function() {
|
||||
@@ -210,6 +261,20 @@ describe("red/util", function() {
|
||||
var result = util.evaluateNodeProperty('^abc$','re');
|
||||
result.toString().should.eql("/^abc$/");
|
||||
});
|
||||
it('returns boolean',function() {
|
||||
var result = util.evaluateNodeProperty('true','bool');
|
||||
result.should.be.true();
|
||||
result = util.evaluateNodeProperty('TrUe','bool');
|
||||
result.should.be.true();
|
||||
result = util.evaluateNodeProperty('false','bool');
|
||||
result.should.be.false();
|
||||
result = util.evaluateNodeProperty('','bool');
|
||||
result.should.be.false();
|
||||
});
|
||||
it('returns date',function() {
|
||||
var result = util.evaluateNodeProperty('','date');
|
||||
(Date.now() - result).should.be.approximately(0,50);
|
||||
});
|
||||
it('returns msg property',function() {
|
||||
var result = util.evaluateNodeProperty('foo.bar','msg',{},{foo:{bar:"123"}});
|
||||
result.should.eql("123");
|
||||
@@ -242,7 +307,48 @@ describe("red/util", function() {
|
||||
},{});
|
||||
result.should.eql("123");
|
||||
});
|
||||
});
|
||||
|
||||
describe('normalisePropertyExpression', function() {
|
||||
function testABC(input,expected) {
|
||||
var result = util.normalisePropertyExpression(input);
|
||||
// console.log("+",input);
|
||||
// console.log(result);
|
||||
result.should.eql(expected);
|
||||
}
|
||||
|
||||
})
|
||||
function testInvalid(input) {
|
||||
/*jshint immed: false */
|
||||
(function() {
|
||||
util.normalisePropertyExpression(input);
|
||||
}).should.throw();
|
||||
}
|
||||
it('pass a.b.c',function() { testABC('a.b.c',['a','b','c']); })
|
||||
it('pass a["b"]["c"]',function() { testABC('a["b"]["c"]',['a','b','c']); })
|
||||
it('pass a["b"].c',function() { testABC('a["b"].c',['a','b','c']); })
|
||||
it("pass a['b'].c",function() { testABC("a['b'].c",['a','b','c']); })
|
||||
|
||||
it("pass a[0].c",function() { testABC("a[0].c",['a',0,'c']); })
|
||||
it("pass a.0.c",function() { testABC("a.0.c",['a',0,'c']); })
|
||||
it("pass a['a.b[0]'].c",function() { testABC("a['a.b[0]'].c",['a','a.b[0]','c']); })
|
||||
it("pass a[0][0][0]",function() { testABC("a[0][0][0]",['a',0,0,0]); })
|
||||
|
||||
it("fail a'b'.c",function() { testInvalid("a'b'.c"); })
|
||||
it("fail a['b'.c",function() { testInvalid("a['b'.c"); })
|
||||
it("fail a[]",function() { testInvalid("a[]"); })
|
||||
it("fail a]",function() { testInvalid("a]"); })
|
||||
it("fail a[",function() { testInvalid("a["); })
|
||||
it("fail a[0d]",function() { testInvalid("a[0d]"); })
|
||||
it("fail a['",function() { testInvalid("a['"); })
|
||||
it("fail a[']",function() { testInvalid("a[']"); })
|
||||
it("fail a[0']",function() { testInvalid("a[0']"); })
|
||||
it("fail a.[0]",function() { testInvalid("a.[0]"); })
|
||||
it("fail [0]",function() { testInvalid("[0]"); })
|
||||
it("fail a[0",function() { testInvalid("a[0"); })
|
||||
it("fail a.",function() { testInvalid("a."); })
|
||||
it("fail .a",function() { testInvalid(".a"); })
|
||||
it("fail a. b",function() { testInvalid("a. b"); })
|
||||
it("fail a.b",function() { testInvalid(" a.b"); })
|
||||
it("fail a[0].[1]",function() { testInvalid("a[0].[1]"); })
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user