Refactor lib/flows code to include initial router component

This commit is contained in:
Nick O'Leary
2020-07-20 16:48:47 +01:00
parent 952c9d8bdb
commit d57ec0cd53
18 changed files with 147 additions and 115 deletions

View File

@@ -0,0 +1,829 @@
/**
* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
var should = require("should");
var sinon = require('sinon');
var clone = require('clone');
var util = require("util");
var NR_TEST_UTILS = require("nr-test-utils");
var flowUtils = NR_TEST_UTILS.require("@node-red/runtime/lib/flows/util");
var Flow = NR_TEST_UTILS.require("@node-red/runtime/lib/flows/Flow");
var flows = NR_TEST_UTILS.require("@node-red/runtime/lib/flows");
var Node = NR_TEST_UTILS.require("@node-red/runtime/lib/nodes/Node");
var typeRegistry = NR_TEST_UTILS.require("@node-red/registry");
describe('Flow', function() {
var getType;
var stoppedNodes = {};
var currentNodes = {};
var rewiredNodes = {};
var createCount = 0;
beforeEach(function() {
currentNodes = {};
stoppedNodes = {};
rewiredNodes = {};
createCount = 0;
Flow.init({settings:{},log:{
log: sinon.stub(), // function() { console.log("l",[...arguments].map(a => JSON.stringify(a)).join(" ")) },//
debug: sinon.stub(), // function() { console.log("d",[...arguments].map(a => JSON.stringify(a)).join(" ")) },//sinon.stub(),
trace: sinon.stub(), // function() { console.log("t",[...arguments].map(a => JSON.stringify(a)).join(" ")) },//sinon.stub(),
warn: sinon.stub(), // function() { console.log("w",[...arguments].map(a => JSON.stringify(a)).join(" ")) },//sinon.stub(),
info: sinon.stub(), // function() { console.log("i",[...arguments].map(a => JSON.stringify(a)).join(" ")) },//sinon.stub(),
metric: sinon.stub(),
_: function() { return "abc"}
}});
});
var TestNode = function(n) {
Node.call(this,n);
this._index = createCount++;
this.scope = n.scope;
var node = this;
this.foo = n.foo;
this.handled = 0;
this.stopped = false;
currentNodes[node.id] = node;
this.on('input',function(msg) {
// console.log(this.id,msg.payload);
node.handled++;
node.send(msg);
});
this.on('close',function() {
node.stopped = true;
stoppedNodes[node.id] = node;
delete currentNodes[node.id];
});
this.__updateWires = this.updateWires;
this.updateWires = function(newWires) {
rewiredNodes[node.id] = node;
node.newWires = newWires;
node.__updateWires(newWires);
};
}
util.inherits(TestNode,Node);
var TestErrorNode = function(n) {
Node.call(this,n);
this._index = createCount++;
this.scope = n.scope;
this.name = n.name;
var node = this;
this.foo = n.foo;
this.handled = 0;
this.stopped = false;
currentNodes[node.id] = node;
this.on('input',function(msg) {
node.handled++;
node.error("test error",msg);
});
this.on('close',function() {
node.stopped = true;
stoppedNodes[node.id] = node;
delete currentNodes[node.id];
});
this.__updateWires = this.updateWires;
this.updateWires = function(newWires) {
rewiredNodes[node.id] = node;
node.newWires = newWires;
node.__updateWires(newWires);
};
}
util.inherits(TestErrorNode,Node);
var TestAsyncNode = function(n) {
Node.call(this,n);
var node = this;
this.scope = n.scope;
this.uncaught = n.uncaught;
this.foo = n.foo;
this.handled = 0;
this.messages = [];
this.stopped = false;
this.closeDelay = n.closeDelay || 50;
currentNodes[node.id] = node;
this.on('input',function(msg) {
node.handled++;
msg.handled = node.handled;
node.messages.push(msg);
node.send(msg);
});
this.on('close',function(done) {
setTimeout(function() {
node.stopped = true;
stoppedNodes[node.id] = node;
delete currentNodes[node.id];
done();
},node.closeDelay);
});
}
util.inherits(TestAsyncNode,Node);
var TestDoneNode = function(n) {
Node.call(this,n);
var node = this;
this.scope = n.scope;
this.uncaught = n.uncaught;
this.foo = n.foo;
this.handled = 0;
this.messages = [];
this.stopped = false;
this.closeDelay = n.closeDelay || 50;
currentNodes[node.id] = node;
this.on('input',function(msg, send, done) {
node.handled++;
node.messages.push(msg);
send(msg);
done();
});
this.on('close',function(done) {
setTimeout(function() {
node.stopped = true;
stoppedNodes[node.id] = node;
delete currentNodes[node.id];
done();
},node.closeDelay);
});
}
util.inherits(TestDoneNode,Node);
before(function() {
getType = sinon.stub(typeRegistry,"get",function(type) {
if (type=="test") {
return TestNode;
} else if (type=="testError"){
return TestErrorNode;
} else if (type=="testDone"){
return TestDoneNode;
} else {
return TestAsyncNode;
}
});
});
after(function() {
getType.restore();
});
describe('#constructor',function() {
it('called with an empty flow',function() {
var config = flowUtils.parseConfig([]);
var flow = Flow.create({},config);
var nodeCount = 0;
Object.keys(flow.getActiveNodes()).length.should.equal(0);
});
});
describe('#start',function() {
it("instantiates an initial configuration and stops it",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:"a"}
]);
var flow = Flow.create({},config,config.flows["t1"]);
flow.start();
Object.keys(flow.getActiveNodes()).should.have.length(4);
flow.getNode('1').should.have.a.property('id','1');
flow.getNode('2').should.have.a.property('id','2');
flow.getNode('3').should.have.a.property('id','3');
flow.getNode('4').should.have.a.property('id','4');
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["1"].should.have.a.property("handled",0);
currentNodes["2"].should.have.a.property("handled",0);
currentNodes["3"].should.have.a.property("handled",0);
currentNodes["3"].on("input", function() {
currentNodes["1"].should.have.a.property("handled",1);
currentNodes["2"].should.have.a.property("handled",1);
currentNodes["3"].should.have.a.property("handled",1);
flow.stop().then(function() {
try {
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");
stoppedNodes.should.have.a.property("1");
stoppedNodes.should.have.a.property("2");
stoppedNodes.should.have.a.property("3");
stoppedNodes.should.have.a.property("4");
done();
} catch(err) {
done(err);
}
});
});
currentNodes["1"].receive({payload:"test"});
});
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("rewires nodes specified by diff",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:[]}
]);
var flow = Flow.create({},config,config.flows["t1"]);
createCount.should.equal(0);
flow.start();
//TODO: use update to pass in new wiring and verify the change
createCount.should.equal(3);
flow.start({rewired:["2"]});
createCount.should.equal(3);
rewiredNodes.should.have.a.property("2");
done();
});
it("instantiates a node with environment variable property values",function(done) {
after(function() {
delete process.env.NODE_RED_TEST_VALUE;
})
process.env.NODE_RED_TEST_VALUE = "a-value";
var config = flowUtils.parseConfig([
{id:"t1",type:"tab"},
{id:"1",x:10,y:10,z:"t1",type:"test",foo:"$(NODE_RED_TEST_VALUE)",wires:[]},
{id:"2",x:10,y:10,z:"t1",type:"test",foo:{a:"$(NODE_RED_TEST_VALUE)"},wires:[]},
{id:"3",x:10,y:10,z:"t1",type:"test",foo:" $(NODE_RED_TEST_VALUE)",wires:[]},
{id:"4",x:10,y:10,z:"t1",type:"test",foo:"$(NODE_RED_TEST_VALUE) ",wires:[]},
{id:"5",x:10,y:10,z:"t1",type:"test",foo:"$(NODE_RED_TEST_VALUE_NONE)",wires:[]},
{id:"6",x:10,y:10,z:"t1",type:"test",foo:["$(NODE_RED_TEST_VALUE)"],wires:[]}
]);
var flow = Flow.create({getSetting:v=>process.env[v]},config,config.flows["t1"]);
flow.start();
var activeNodes = flow.getActiveNodes();
activeNodes["1"].foo.should.equal("a-value");
activeNodes["2"].foo.a.should.equal("a-value");
activeNodes["3"].foo.should.equal(" $(NODE_RED_TEST_VALUE)");
activeNodes["4"].foo.should.equal("$(NODE_RED_TEST_VALUE) ");
activeNodes["5"].foo.should.equal("$(NODE_RED_TEST_VALUE_NONE)");
activeNodes["6"].foo[0].should.equal("a-value");
flow.stop().then(function() {
done();
});
});
it("ignores disabled nodes",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",d:true,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:"a"},
{id:"5",z:"t1",type:"test",d:true,foo:"a"}
]);
var flow = Flow.create({},config,config.flows["t1"]);
flow.start();
Object.keys(flow.getActiveNodes()).should.have.length(3);
flow.getNode('1').should.have.a.property('id','1');
should.not.exist(flow.getNode('2'));
flow.getNode('3').should.have.a.property('id','3');
flow.getNode('4').should.have.a.property('id','4');
should.not.exist(flow.getNode('5'));
currentNodes.should.have.a.property("1");
currentNodes.should.not.have.a.property("2");
currentNodes.should.have.a.property("3");
currentNodes.should.have.a.property("4");
currentNodes["1"].should.have.a.property("handled",0);
currentNodes["3"].should.have.a.property("handled",0);
currentNodes["1"].receive({payload:"test"});
setTimeout(function() {
currentNodes["1"].should.have.a.property("handled",1);
// Message doesn't reach 3 as 2 is disabled
currentNodes["3"].should.have.a.property("handled",0);
flow.stop().then(function() {
try {
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");
stoppedNodes.should.have.a.property("1");
stoppedNodes.should.not.have.a.property("2");
stoppedNodes.should.have.a.property("3");
stoppedNodes.should.have.a.property("4");
done();
} catch(err) {
done(err);
}
});
},50);
});
});
describe('#stop', function() {
it("stops all nodes",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:"asyncTest",foo:"a",wires:[]}
]);
var flow = Flow.create({},config,config.flows["t1"]);
flow.start();
currentNodes.should.have.a.property("1");
currentNodes.should.have.a.property("2");
currentNodes.should.have.a.property("3");
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");
stoppedNodes.should.have.a.property("1");
stoppedNodes.should.have.a.property("2");
stoppedNodes.should.have.a.property("3");
done();
}).catch(done);
});
it("stops specified nodes",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:[]}
]);
var flow = Flow.create({},config,config.flows["t1"]);
flow.start();
currentNodes.should.have.a.property("1");
currentNodes.should.have.a.property("2");
currentNodes.should.have.a.property("3");
flow.stop(["2"]).then(function() {
currentNodes.should.have.a.property("1");
currentNodes.should.not.have.a.property("2");
currentNodes.should.have.a.property("3");
stoppedNodes.should.not.have.a.property("1");
stoppedNodes.should.have.a.property("2");
stoppedNodes.should.not.have.a.property("3");
done();
});
});
it("Times out a node that fails to close", function(done) {
Flow.init({settings:{nodeCloseTimeout:50},log:{
log: sinon.stub(),
debug: sinon.stub(),
trace: sinon.stub(),
warn: sinon.stub(),
info: sinon.stub(),
metric: sinon.stub(),
_: function() { return "abc"}
}});
var config = flowUtils.parseConfig([
{id:"t1",type:"tab"},
{id:"1",x:10,y:10,z:"t1",type:"testAsync",closeDelay: 80, 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:[]}
]);
var flow = Flow.create({},config,config.flows["t1"]);
flow.start();
currentNodes.should.have.a.property("1");
currentNodes.should.have.a.property("2");
currentNodes.should.have.a.property("3");
flow.stop().then(function() {
currentNodes.should.have.a.property("1");
currentNodes.should.not.have.a.property("2");
currentNodes.should.not.have.a.property("3");
stoppedNodes.should.not.have.a.property("1");
stoppedNodes.should.have.a.property("2");
stoppedNodes.should.have.a.property("3");
setTimeout(function() {
currentNodes.should.not.have.a.property("1");
stoppedNodes.should.have.a.property("1");
done();
},40)
});
});
});
describe('#getNode',function() {
it("gets a node known to the flow",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:"a"}
]);
var flow = Flow.create({},config,config.flows["t1"]);
flow.start();
Object.keys(flow.getActiveNodes()).should.have.length(4);
flow.getNode('1').should.have.a.property('id','1');
flow.stop().then(() => { done() });
});
it("passes to parent if node not known locally",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:"a"}
]);
var flow = Flow.create({
getNode: id => { return {id:id}}
},config,config.flows["t1"]);
flow.start();
Object.keys(flow.getActiveNodes()).should.have.length(4);
flow.getNode('1').should.have.a.property('id','1');
flow.getNode('parentNode').should.have.a.property('id','parentNode');
flow.stop().then(() => { done() });
});
it("does not pass to parent if cancelBubble set",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:"a"}
]);
var flow = Flow.create({
getNode: id => { return {id:id}}
},config,config.flows["t1"]);
flow.start();
Object.keys(flow.getActiveNodes()).should.have.length(4);
flow.getNode('1').should.have.a.property('id','1');
should.not.exist(flow.getNode('parentNode',true));
flow.stop().then(() => { done() });
});
});
describe("#handleStatus",function() {
it("passes a status event to the adjacent status node",function(done) {
var config = flowUtils.parseConfig([
{id:"t1",type:"tab"},
{id:"1",x:10,y:10,z:"t1",type:"test",name:"a",wires:["2"]},
{id:"2",x:10,y:10,z:"t1",type:"test",wires:["3"]},
{id:"3",x:10,y:10,z:"t1",type:"test",foo:"a",wires:[]},
{id:"sn",x:10,y:10,z:"t1",type:"status",foo:"a",wires:[]},
{id:"sn2",x:10,y:10,z:"t1",type:"status",foo:"a",wires:[]}
]);
var flow = Flow.create({},config,config.flows["t1"]);
flow.start();
var activeNodes = flow.getActiveNodes();
Object.keys(activeNodes).should.have.length(5);
flow.handleStatus(config.flows["t1"].nodes["1"],{text:"my-status",random:"otherProperty"});
setTimeout(function() {
currentNodes["sn"].should.have.a.property("handled",1);
var statusMessage = currentNodes["sn"].messages[0];
statusMessage.should.have.a.property("status");
statusMessage.status.should.have.a.property("text","my-status");
statusMessage.status.should.have.a.property("source");
statusMessage.status.source.should.have.a.property("id","1");
statusMessage.status.source.should.have.a.property("type","test");
statusMessage.status.source.should.have.a.property("name","a");
currentNodes["sn2"].should.have.a.property("handled",1);
statusMessage = currentNodes["sn2"].messages[0];
statusMessage.should.have.a.property("status");
statusMessage.status.should.have.a.property("text","my-status");
statusMessage.status.should.have.a.property("random","otherProperty");
statusMessage.status.should.have.a.property("source");
statusMessage.status.source.should.have.a.property("id","1");
statusMessage.status.source.should.have.a.property("type","test");
statusMessage.status.source.should.have.a.property("name","a");
flow.stop().then(function() {
done();
});
},50)
});
it("passes a status event to the adjacent scoped status node ",function(done) {
var config = flowUtils.parseConfig([
{id:"t1",type:"tab"},
{id:"1",x:10,y:10,z:"t1",type:"test",name:"a",wires:["2"]},
{id:"2",x:10,y:10,z:"t1",type:"test",wires:["3"]},
{id:"3",x:10,y:10,z:"t1",type:"test",foo:"a",wires:[]},
{id:"sn",x:10,y:10,z:"t1",type:"status",scope:["2"],foo:"a",wires:[]},
{id:"sn2",x:10,y:10,z:"t1",type:"status",scope:["1"],foo:"a",wires:[]}
]);
var flow = Flow.create({},config,config.flows["t1"]);
flow.start();
var activeNodes = flow.getActiveNodes();
Object.keys(activeNodes).should.have.length(5);
flow.handleStatus(config.flows["t1"].nodes["1"],{text:"my-status"});
setTimeout(function() {
currentNodes["sn"].should.have.a.property("handled",0);
currentNodes["sn2"].should.have.a.property("handled",1);
var statusMessage = currentNodes["sn2"].messages[0];
statusMessage.should.have.a.property("status");
statusMessage.status.should.have.a.property("text","my-status");
statusMessage.status.should.have.a.property("source");
statusMessage.status.source.should.have.a.property("id","1");
statusMessage.status.source.should.have.a.property("type","test");
statusMessage.status.source.should.have.a.property("name","a");
flow.stop().then(function() {
done();
});
},50);
});
});
describe("#handleError",function() {
it("passes an error event to the adjacent catch node",function(done) {
var config = flowUtils.parseConfig([
{id:"t1",type:"tab"},
{id:"1",x:10,y:10,z:"t1",type:"test",name:"a",wires:["2"]},
{id:"2",x:10,y:10,z:"t1",type:"test",wires:["3"]},
{id:"3",x:10,y:10,z:"t1",type:"test",foo:"a",wires:[]},
{id:"sn",x:10,y:10,z:"t1",type:"catch",foo:"a",wires:[]},
{id:"sn2",x:10,y:10,z:"t1",type:"catch",foo:"a",wires:[]},
{id:"sn3",x:10,y:10,z:"t1",type:"catch",uncaught:true,wires:[]}
]);
var flow = Flow.create({},config,config.flows["t1"]);
flow.start();
var activeNodes = flow.getActiveNodes();
Object.keys(activeNodes).should.have.length(6);
flow.handleError(config.flows["t1"].nodes["1"],"my-error",{a:"foo"});
setTimeout(function() {
currentNodes["sn"].should.have.a.property("handled",1);
var statusMessage = currentNodes["sn"].messages[0];
statusMessage.should.have.a.property("error");
statusMessage.error.should.have.a.property("message","my-error");
statusMessage.error.should.have.a.property("source");
statusMessage.error.source.should.have.a.property("id","1");
statusMessage.error.source.should.have.a.property("type","test");
statusMessage.error.source.should.have.a.property("name","a");
currentNodes["sn2"].should.have.a.property("handled",1);
statusMessage = currentNodes["sn2"].messages[0];
statusMessage.should.have.a.property("error");
statusMessage.error.should.have.a.property("message","my-error");
statusMessage.error.should.have.a.property("source");
statusMessage.error.source.should.have.a.property("id","1");
statusMessage.error.source.should.have.a.property("type","test");
statusMessage.error.source.should.have.a.property("name","a");
// Node sn3 has uncaught:true - so should not get called
currentNodes["sn3"].should.have.a.property("handled",0);
flow.stop().then(function() {
done();
});
},50);
});
it("passes an error event to the adjacent scoped catch node ",function(done) {
var config = flowUtils.parseConfig([
{id:"t1",type:"tab"},
{id:"1",x:10,y:10,z:"t1",type:"test",name:"a",wires:["2"]},
{id:"2",x:10,y:10,z:"t1",type:"test",wires:["3"]},
{id:"3",x:10,y:10,z:"t1",type:"test",foo:"a",wires:[]},
{id:"sn",x:10,y:10,z:"t1",type:"catch",scope:["2"],foo:"a",wires:[]},
{id:"sn2",x:10,y:10,z:"t1",type:"catch",scope:["1"],foo:"a",wires:[]},
{id:"sn3",x:10,y:10,z:"t1",type:"catch",uncaught:true,wires:[]},
{id:"sn4",x:10,y:10,z:"t1",type:"catch",uncaught:true,wires:[]}
]);
var flow = Flow.create({},config,config.flows["t1"]);
flow.start();
var activeNodes = flow.getActiveNodes();
Object.keys(activeNodes).should.have.length(7);
flow.handleError(config.flows["t1"].nodes["1"],"my-error",{a:"foo"});
setTimeout(function() {
currentNodes["sn"].should.have.a.property("handled",0);
currentNodes["sn2"].should.have.a.property("handled",1);
var statusMessage = currentNodes["sn2"].messages[0];
statusMessage.should.have.a.property("error");
statusMessage.error.should.have.a.property("message","my-error");
statusMessage.error.should.have.a.property("source");
statusMessage.error.source.should.have.a.property("id","1");
statusMessage.error.source.should.have.a.property("type","test");
statusMessage.error.source.should.have.a.property("name","a");
// Node sn3/4 have uncaught:true - so should not get called
currentNodes["sn3"].should.have.a.property("handled",0);
currentNodes["sn4"].should.have.a.property("handled",0);
// Inject error that sn1/2 will ignore - so should get picked up by sn3
flow.handleError(config.flows["t1"].nodes["3"],"my-error-2",{a:"foo-2"});
setTimeout(function() {
currentNodes["sn"].should.have.a.property("handled",0);
currentNodes["sn2"].should.have.a.property("handled",1);
currentNodes["sn3"].should.have.a.property("handled",1);
currentNodes["sn4"].should.have.a.property("handled",1);
statusMessage = currentNodes["sn3"].messages[0];
statusMessage.should.have.a.property("error");
statusMessage.error.should.have.a.property("message","my-error-2");
statusMessage.error.should.have.a.property("source");
statusMessage.error.source.should.have.a.property("id","3");
statusMessage.error.source.should.have.a.property("type","test");
flow.stop().then(function() {
done();
});
},50);
},50);
});
it("moves any existing error object sideways",function(done){
var config = flowUtils.parseConfig([
{id:"t1",type:"tab"},
{id:"1",x:10,y:10,z:"t1",type:"test",name:"a",wires:["2"]},
{id:"2",x:10,y:10,z:"t1",type:"test",wires:["3"]},
{id:"3",x:10,y:10,z:"t1",type:"test",foo:"a",wires:[]},
{id:"sn",x:10,y:10,z:"t1",type:"catch",foo:"a",wires:[]}
]);
var flow = Flow.create({},config,config.flows["t1"]);
flow.start();
var activeNodes = flow.getActiveNodes();
flow.handleError(config.flows["t1"].nodes["1"],"my-error",{a:"foo",error:"existing"});
setTimeout(function() {
currentNodes["sn"].should.have.a.property("handled",1);
var statusMessage = currentNodes["sn"].messages[0];
statusMessage.should.have.a.property("_error","existing");
statusMessage.should.have.a.property("error");
statusMessage.error.should.have.a.property("message","my-error");
statusMessage.error.should.have.a.property("source");
statusMessage.error.source.should.have.a.property("id","1");
statusMessage.error.source.should.have.a.property("type","test");
statusMessage.error.source.should.have.a.property("name","a");
flow.stop().then(function() {
done();
});
},50);
});
it("prevents an error looping more than 10 times",function(){});
});
describe("#handleComplete",function() {
it("passes a complete event to the adjacent Complete node",function(done) {
var config = flowUtils.parseConfig([
{id:"t1",type:"tab"},
{id:"1",x:10,y:10,z:"t1",type:"testDone",name:"a",wires:["2"]},
{id:"2",x:10,y:10,z:"t1",type:"test",wires:["3"]},
{id:"3",x:10,y:10,z:"t1",type:"testDone",foo:"a",wires:[]},
{id:"cn",x:10,y:10,z:"t1",type:"complete",scope:["1","3"],foo:"a",wires:[]}
]);
var flow = Flow.create({},config,config.flows["t1"]);
flow.start();
var activeNodes = flow.getActiveNodes();
Object.keys(activeNodes).should.have.length(4);
var msg = {payload: "hello world"}
var n1 = currentNodes["1"].receive(msg);
setTimeout(function() {
currentNodes["cn"].should.have.a.property("handled",2);
currentNodes["cn"].messages[0].should.have.a.property("handled",1);
currentNodes["cn"].messages[1].should.have.a.property("handled",2);
flow.stop().then(function() {
done();
});
},50);
});
});
});

View File

@@ -0,0 +1,885 @@
/**
* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
var should = require("should");
var sinon = require('sinon');
var clone = require('clone');
var util = require("util");
var NR_TEST_UTILS = require("nr-test-utils");
var Subflow = NR_TEST_UTILS.require("@node-red/runtime/lib/flows/Subflow");
var Flow = NR_TEST_UTILS.require("@node-red/runtime/lib/flows/Flow");
var flowUtils = NR_TEST_UTILS.require("@node-red/runtime/lib/flows/util");
var flows = NR_TEST_UTILS.require("@node-red/runtime/lib/flows");
var Node = NR_TEST_UTILS.require("@node-red/runtime/lib/nodes/Node");
var typeRegistry = NR_TEST_UTILS.require("@node-red/registry");
describe('Subflow', function() {
var getType;
var stoppedNodes = {};
var currentNodes = {};
var rewiredNodes = {};
var createCount = 0;
beforeEach(function() {
currentNodes = {};
stoppedNodes = {};
rewiredNodes = {};
createCount = 0;
var runtime = {
settings:{},
log:{
log: sinon.stub(), // function() { console.log("l",[...arguments].map(a => JSON.stringify(a)).join(" ")) },//
debug: sinon.stub(), // function() { console.log("d",[...arguments].map(a => JSON.stringify(a)).join(" ")) },//sinon.stub(),
trace: sinon.stub(), // function() { console.log("t",[...arguments].map(a => JSON.stringify(a)).join(" ")) },//sinon.stub(),
warn: sinon.stub(), // function() { console.log("w",[...arguments].map(a => JSON.stringify(a)).join(" ")) },//sinon.stub(),
info: sinon.stub(), // function() { console.log("i",[...arguments].map(a => JSON.stringify(a)).join(" ")) },//sinon.stub(),
metric: sinon.stub(),
_: function() { return "abc"}
}
}
Flow.init(runtime);
Subflow.init(runtime);
});
var TestNode = function(n) {
Node.call(this,n);
this._index = createCount++;
this.scope = n.scope;
var node = this;
this.foo = n.foo;
this.handled = 0;
this.stopped = false;
this.received = null;
currentNodes[node.id] = node;
this.on('input',function(msg) {
// console.log(this.id,msg.payload);
node.handled++;
node.received = msg.payload;
node.send(msg);
});
this.on('close',function() {
node.stopped = true;
stoppedNodes[node.id] = node;
delete currentNodes[node.id];
});
this.__updateWires = this.updateWires;
this.updateWires = function(newWires) {
rewiredNodes[node.id] = node;
node.newWires = newWires;
node.__updateWires(newWires);
};
}
util.inherits(TestNode,Node);
var TestErrorNode = function(n) {
Node.call(this,n);
this._index = createCount++;
this.scope = n.scope;
this.name = n.name;
var node = this;
this.foo = n.foo;
this.handled = 0;
this.stopped = false;
currentNodes[node.id] = node;
this.on('input',function(msg) {
node.handled++;
node.error("test error",msg);
});
this.on('close',function() {
node.stopped = true;
stoppedNodes[node.id] = node;
delete currentNodes[node.id];
});
this.__updateWires = this.updateWires;
this.updateWires = function(newWires) {
rewiredNodes[node.id] = node;
node.newWires = newWires;
node.__updateWires(newWires);
};
}
util.inherits(TestErrorNode,Node);
var TestStatusNode = function(n) {
Node.call(this,n);
this._index = createCount++;
this.scope = n.scope;
this.name = n.name;
var node = this;
this.foo = n.foo;
this.handled = 0;
this.stopped = false;
currentNodes[node.id] = node;
this.on('input',function(msg) {
node.handled++;
node.status({text:"test status"});
});
this.on('close',function() {
node.stopped = true;
stoppedNodes[node.id] = node;
delete currentNodes[node.id];
});
this.__updateWires = this.updateWires;
this.updateWires = function(newWires) {
rewiredNodes[node.id] = node;
node.newWires = newWires;
node.__updateWires(newWires);
};
}
util.inherits(TestStatusNode,Node);
var TestAsyncNode = function(n) {
Node.call(this,n);
var node = this;
this.scope = n.scope;
this.foo = n.foo;
this.handled = 0;
this.messages = [];
this.stopped = false;
this.closeDelay = n.closeDelay || 50;
currentNodes[node.id] = node;
this.on('input',function(msg) {
node.handled++;
node.messages.push(msg);
node.send(msg);
});
this.on('close',function(done) {
setTimeout(function() {
node.stopped = true;
stoppedNodes[node.id] = node;
delete currentNodes[node.id];
done();
},node.closeDelay);
});
}
util.inherits(TestAsyncNode,Node);
var TestEnvNode = function(n) {
Node.call(this,n);
this._index = createCount++;
this.scope = n.scope;
this.foo = n.foo;
var node = this;
this.stopped = false;
this.received = null;
currentNodes[node.id] = node;
this.on('input',function(msg) {
var flow = node._flow;
var val = flow.getSetting("__KEY__");
node.received = val;
node.send({payload: val});
});
this.on('close',function() {
node.stopped = true;
stoppedNodes[node.id] = node;
delete currentNodes[node.id];
});
this.__updateWires = this.updateWires;
this.updateWires = function(newWires) {
rewiredNodes[node.id] = node;
node.newWires = newWires;
node.__updateWires(newWires);
};
}
util.inherits(TestEnvNode,Node);
before(function() {
getType = sinon.stub(typeRegistry,"get",function(type) {
if (type=="test") {
return TestNode;
} else if (type=="testError"){
return TestErrorNode;
} else if (type=="testStatus"){
return TestStatusNode;
} else if (type=="testEnv"){
return TestEnvNode;
} else {
return TestAsyncNode;
}
});
});
after(function() {
getType.restore();
});
describe('#start',function() {
it("instantiates a subflow and stops it",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:"subflow:sf1",wires:["3","4"]},
{id:"3",x:10,y:10,z:"t1",type:"test",foo:"a",wires:[]},
{id:"4",x:10,y:10,z:"t1",type:"test",foo:"a",wires:[]},
{id:"sf1",type:"subflow","name":"Subflow 2","info":"",
"in":[{"wires":[{"id":"sf1-1"}]}],"out":[{"wires":[{"id":"sf1-2","port":0}]},{"wires":[{"id":"sf1","port":0}]}]},
{id:"sf1-1",type:"test","z":"sf1",x:166,y:99,"wires":[["sf1-2"]]},
{id:"sf1-2",type:"test","z":"sf1",foo:"sf1-cn",x:166,y:99,"wires":[[]]},
{id:"sf1-cn",type:"test","z":"sf1"}
]);
var flow = Flow.create({handleError: (a,b,c) => { console.log(a,b,c); }},config,config.flows["t1"]);
flow.start();
var activeNodes = flow.getActiveNodes();
Object.keys(activeNodes).should.have.length(4);
// var sfInstanceId = Object.keys(activeNodes)[5];
// var sfInstanceId2 = Object.keys(activeNodes)[6];
var sfConfigId = Object.keys(activeNodes)[4];
flow.getNode('1').should.have.a.property('id','1');
flow.getNode('2').should.have.a.property('id','2');
flow.getNode('3').should.have.a.property('id','3');
flow.getNode('4').should.have.a.property('id','4');
// flow.getNode(sfInstanceId).should.have.a.property('id',sfInstanceId);
// flow.getNode(sfInstanceId2).should.have.a.property('id',sfInstanceId2);
// flow.getNode(sfConfigId).should.have.a.property('id',sfConfigId);
// flow.getNode(sfInstanceId2).should.have.a.property('foo',sfConfigId);
Object.keys(currentNodes).should.have.length(6);
currentNodes.should.have.a.property("1");
currentNodes.should.not.have.a.property("2");
currentNodes.should.have.a.property("3");
currentNodes.should.have.a.property("4");
// currentNodes.should.have.a.property(sfInstanceId);
// currentNodes.should.have.a.property(sfInstanceId2);
// currentNodes.should.have.a.property(sfConfigId);
currentNodes["1"].should.have.a.property("handled",0);
currentNodes["3"].should.have.a.property("handled",0);
currentNodes["4"].should.have.a.property("handled",0);
// currentNodes[sfInstanceId].should.have.a.property("handled",0);
// currentNodes[sfInstanceId2].should.have.a.property("handled",0);
currentNodes["1"].receive({payload:"test"});
setTimeout(function() {
currentNodes["1"].should.have.a.property("handled",1);
// currentNodes[sfInstanceId].should.have.a.property("handled",1);
// currentNodes[sfInstanceId2].should.have.a.property("handled",1);
currentNodes["3"].should.have.a.property("handled",1);
currentNodes["4"].should.have.a.property("handled",1);
flow.stop().then(function() {
Object.keys(currentNodes).should.have.length(0);
Object.keys(stoppedNodes).should.have.length(6);
// currentNodes.should.not.have.a.property("1");
// currentNodes.should.not.have.a.property("3");
// currentNodes.should.not.have.a.property("4");
// // currentNodes.should.not.have.a.property(sfInstanceId);
// // currentNodes.should.not.have.a.property(sfInstanceId2);
// // currentNodes.should.not.have.a.property(sfConfigId);
// stoppedNodes.should.have.a.property("1");
// stoppedNodes.should.have.a.property("3");
// stoppedNodes.should.have.a.property("4");
// // stoppedNodes.should.have.a.property(sfInstanceId);
// // stoppedNodes.should.have.a.property(sfInstanceId2);
// // stoppedNodes.should.have.a.property(sfConfigId);
done();
});
},150);
});
it("instantiates a subflow inside a subflow and stops it",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:"subflow:sf1",wires:["3","4"]},
{id:"3",x:10,y:10,z:"t1",type:"test",foo:"a",wires:[]},
{id:"4",x:10,y:10,z:"t1",type:"test",foo:"a",wires:[]},
{id:"sf1",type:"subflow","name":"Subflow 1","info":"",
"in":[{"wires":[{"id":"sf1-1"}]}],"out":[{"wires":[{"id":"sf1-2","port":0}]}]},
{id:"sf2",type:"subflow","name":"Subflow 2","info":"",
"in":[{wires:[]}],"out":[{"wires":[{"id":"sf2","port":0}]}]},
{id:"sf1-1",type:"test","z":"sf1",x:166,y:99,"wires":[["sf1-2"]]},
{id:"sf1-2",type:"subflow:sf2","z":"sf1",x:166,y:99,"wires":[[]]}
]);
var flow = Flow.create({},config,config.flows["t1"]);
flow.start();
currentNodes["1"].should.have.a.property("handled",0);
currentNodes["3"].should.have.a.property("handled",0);
currentNodes["1"].receive({payload:"test"});
setTimeout(function() {
currentNodes["1"].should.have.a.property("handled",1);
currentNodes["3"].should.have.a.property("handled",1);
flow.stop().then(function() {
Object.keys(currentNodes).should.have.length(0);
done();
});
},150);
});
it("rewires a subflow node on update/start",function(done){
var rawConfig = [
{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:"subflow:sf1",wires:["3"]},
{id:"3",x:10,y:10,z:"t1",type:"test",foo:"a",wires:[]},
{id:"4",x:10,y:10,z:"t1",type:"test",foo:"a",wires:[]},
{id:"sf1",type:"subflow","name":"Subflow 2","info":"",
"in":[{"wires":[{"id":"sf1-1"}]}],"out":[{"wires":[{"id":"sf1-2","port":0}]}]},
{id:"sf1-1",type:"test1","z":"sf1",x:166,y:99,"wires":[["sf1-2"]]},
{id:"sf1-2",type:"test2","z":"sf1",x:166,y:99,"wires":[[]]}
];
var config = flowUtils.parseConfig(clone(rawConfig));
rawConfig[2].wires = [["4"]];
var newConfig = flowUtils.parseConfig(rawConfig);
var diff = flowUtils.diffConfigs(config,newConfig);
var flow = Flow.create({},config,config.flows["t1"]);
flow.start();
var activeNodes = flow.getActiveNodes();
Object.keys(activeNodes).should.have.length(4);
// var sfInstanceId = Object.keys(activeNodes)[4];
// var sfInstanceId2 = Object.keys(activeNodes)[5];
currentNodes["1"].should.have.a.property("handled",0);
currentNodes["3"].should.have.a.property("handled",0);
currentNodes["4"].should.have.a.property("handled",0);
currentNodes["1"].receive({payload:"test"});
setTimeout(function() {
currentNodes["1"].should.have.a.property("handled",1);
// currentNodes[sfInstanceId].should.have.a.property("handled",1);
// currentNodes[sfInstanceId2].should.have.a.property("handled",1);
currentNodes["3"].should.have.a.property("handled",1);
currentNodes["4"].should.have.a.property("handled",0);
flow.update(newConfig,newConfig.flows["t1"]);
flow.start(diff)
currentNodes["1"].receive({payload:"test2"});
setTimeout(function() {
currentNodes["1"].should.have.a.property("handled",2);
// currentNodes[sfInstanceId].should.have.a.property("handled",2);
// currentNodes[sfInstanceId2].should.have.a.property("handled",2);
currentNodes["3"].should.have.a.property("handled",1);
currentNodes["4"].should.have.a.property("handled",1);
flow.stop().then(function() {
done();
});
},150);
},150);
});
});
describe('#stop', function() {
it("stops subflow instance nodes",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:"subflow:sf1",wires:["3"]},
{id:"3",x:10,y:10,z:"t1",type:"test",foo:"a",wires:[]},
{id:"sf1",type:"subflow","name":"Subflow 2","info":"",
"in":[{"wires":[{"id":"sf1-1"}]}],"out":[{"wires":[{"id":"sf1-1","port":0}]}]},
{id:"sf1-1",type:"test","z":"sf1",x:166,y:99,"wires":[[]]}
]);
var flow = Flow.create({},config,config.flows["t1"]);
flow.start();
var activeNodes = flow.getActiveNodes();
Object.keys(activeNodes).should.have.length(3);
Object.keys(stoppedNodes).should.have.length(0);
flow.stop(["2"]).then(function() {
Object.keys(currentNodes).should.have.length(2);
Object.keys(stoppedNodes).should.have.length(1);
done();
}).catch(done);
});
});
describe("#handleStatus",function() {
it("passes a status event to the subflow's parent tab status node - all scope",function(done) {
var config = flowUtils.parseConfig([
{id:"t1",type:"tab"},
{id:"1",x:10,y:10,z:"t1",type:"test",name:"a",wires:["2"]},
{id:"2",x:10,y:10,z:"t1",type:"subflow:sf1",wires:["3"]},
{id:"3",x:10,y:10,z:"t1",type:"test",foo:"a",wires:[]},
{id:"sf1",type:"subflow","name":"Subflow 2","info":"",
"in":[{"wires":[{"id":"sf1-1"}]}],"out":[{"wires":[{"id":"sf1-1","port":0}]}]},
{id:"sf1-1",type:"testStatus",name:"test-status-node","z":"sf1",x:166,y:99,"wires":[[]]},
{id:"sn",x:10,y:10,z:"t1",type:"status",foo:"a",wires:[]}
]);
var flow = Flow.create({},config,config.flows["t1"]);
flow.start();
var activeNodes = flow.getActiveNodes();
activeNodes["1"].receive({payload:"test"});
setTimeout(function() {
currentNodes["sn"].should.have.a.property("handled",1);
var statusMessage = currentNodes["sn"].messages[0];
statusMessage.should.have.a.property("status");
statusMessage.status.should.have.a.property("text","test status");
statusMessage.status.should.have.a.property("source");
statusMessage.status.source.should.have.a.property("type","testStatus");
statusMessage.status.source.should.have.a.property("name","test-status-node");
flow.stop().then(function() {
done();
});
},150);
});
it("passes a status event to the subflow's parent tab status node - targetted scope",function(done) {
var config = flowUtils.parseConfig([
{id:"t1",type:"tab"},
{id:"1",x:10,y:10,z:"t1",type:"test",name:"a",wires:["2"]},
{id:"2",x:10,y:10,z:"t1",type:"subflow:sf1",wires:["3"]},
{id:"3",x:10,y:10,z:"t1",type:"test",foo:"a",wires:[]},
{id:"sf1",type:"subflow","name":"Subflow 2","info":"",
"in":[{"wires":[{"id":"sf1-1"}]}],"out":[{"wires":[{"id":"sf1-1","port":0}]}]},
{id:"sf1-1",type:"testStatus",name:"test-status-node","z":"sf1",x:166,y:99,"wires":[[]]},
{id:"sn",x:10,y:10,z:"t1",type:"status",scope:["2"],wires:[]}
]);
var parentFlowStatusCalled = false;
var flow = Flow.create({handleStatus:() => { parentFlowStatusCalled = true} },config,config.flows["t1"]);
flow.start();
var activeNodes = flow.getActiveNodes();
activeNodes["1"].receive({payload:"test"});
setTimeout(function() {
parentFlowStatusCalled.should.be.false();
currentNodes["sn"].should.have.a.property("handled",1);
var statusMessage = currentNodes["sn"].messages[0];
statusMessage.should.have.a.property("status");
statusMessage.status.should.have.a.property("text","test status");
statusMessage.status.should.have.a.property("source");
statusMessage.status.source.should.have.a.property("type","testStatus");
statusMessage.status.source.should.have.a.property("name","test-status-node");
flow.stop().then(function() {
done();
});
},150);
});
});
describe("status node", function() {
it("emits a status event when a message is passed to a subflow-status node - msg.payload as string", function(done) {
var config = flowUtils.parseConfig([
{id:"t1",type:"tab"},
{id:"1",x:10,y:10,z:"t1",type:"test",name:"a",wires:["2"]},
{id:"2",x:10,y:10,z:"t1",type:"subflow:sf1",wires:["3"]},
{id:"3",x:10,y:10,z:"t1",type:"test",foo:"a",wires:[]},
{
id:"sf1",
type:"subflow",
name:"Subflow 2",
info:"",
in:[{wires:[{id:"sf1-1"}]}],
out:[{wires:[{id:"sf1-1",port:0}]}],
status:{wires:[{id:"sf1-1", port:0}]}
},
{id:"sf1-1",type:"test",name:"test","z":"sf1",x:166,y:99,"wires":[[]]},
{id:"sn",x:10,y:10,z:"t1",type:"status",foo:"a",wires:[]}
]);
var flow = Flow.create({},config,config.flows["t1"]);
flow.start();
var activeNodes = flow.getActiveNodes();
activeNodes["1"].receive({payload:"test-payload"});
setTimeout(function() {
currentNodes["sn"].should.have.a.property("handled",1);
var statusMessage = currentNodes["sn"].messages[0];
statusMessage.should.have.a.property("status");
statusMessage.status.should.have.a.property("text","test-payload");
statusMessage.status.should.have.a.property("source");
statusMessage.status.source.should.have.a.property("id","2");
statusMessage.status.source.should.have.a.property("type","subflow:sf1");
flow.stop().then(function() {
done();
});
},150);
});
it("emits a status event when a message is passed to a subflow-status node - msg.payload as status obj", function(done) {
var config = flowUtils.parseConfig([
{id:"t1",type:"tab"},
{id:"1",x:10,y:10,z:"t1",type:"test",name:"a",wires:["2"]},
{id:"2",x:10,y:10,z:"t1",type:"subflow:sf1",wires:["3"]},
{id:"3",x:10,y:10,z:"t1",type:"test",foo:"a",wires:[]},
{
id:"sf1",
type:"subflow",
name:"Subflow 2",
info:"",
in:[{wires:[{id:"sf1-1"}]}],
out:[{wires:[{id:"sf1-1",port:0}]}],
status:{wires:[{id:"sf1-1", port:0}]}
},
{id:"sf1-1",type:"test",name:"test","z":"sf1",x:166,y:99,"wires":[[]]},
{id:"sn",x:10,y:10,z:"t1",type:"status",foo:"a",wires:[]}
]);
var flow = Flow.create({},config,config.flows["t1"]);
flow.start();
var activeNodes = flow.getActiveNodes();
activeNodes["1"].receive({payload:{text:"payload-obj"}});
setTimeout(function() {
currentNodes["sn"].should.have.a.property("handled",1);
var statusMessage = currentNodes["sn"].messages[0];
statusMessage.should.have.a.property("status");
statusMessage.status.should.have.a.property("text","payload-obj");
statusMessage.status.should.have.a.property("source");
statusMessage.status.source.should.have.a.property("id","2");
statusMessage.status.source.should.have.a.property("type","subflow:sf1");
flow.stop().then(function() {
done();
});
},150);
});
it("emits a status event when a message is passed to a subflow-status node - msg.status", function(done) {
var config = flowUtils.parseConfig([
{id:"t1",type:"tab"},
{id:"1",x:10,y:10,z:"t1",type:"test",name:"a",wires:["2"]},
{id:"2",x:10,y:10,z:"t1",type:"subflow:sf1",wires:["3"]},
{id:"3",x:10,y:10,z:"t1",type:"test",foo:"a",wires:[]},
{
id:"sf1",
type:"subflow",
name:"Subflow 2",
info:"",
in:[{wires:[{id:"sf1-1"}]}],
out:[{wires:[{id:"sf1-1",port:0}]}],
status:{wires:[{id:"sf1-1", port:0}]}
},
{id:"sf1-1",type:"test",name:"test","z":"sf1",x:166,y:99,"wires":[[]]},
{id:"sn",x:10,y:10,z:"t1",type:"status",foo:"a",wires:[]}
]);
var flow = Flow.create({},config,config.flows["t1"]);
flow.start();
var activeNodes = flow.getActiveNodes();
activeNodes["1"].receive({status:{text:"status-obj"}});
setTimeout(function() {
currentNodes["sn"].should.have.a.property("handled",1);
var statusMessage = currentNodes["sn"].messages[0];
statusMessage.should.have.a.property("status");
statusMessage.status.should.have.a.property("text","status-obj");
statusMessage.status.should.have.a.property("source");
statusMessage.status.source.should.have.a.property("id","2");
statusMessage.status.source.should.have.a.property("type","subflow:sf1");
flow.stop().then(function() {
done();
});
},150);
});
it("does not emit a regular status event if it contains a subflow-status node", function(done) {
var config = flowUtils.parseConfig([
{id:"t1",type:"tab"},
{id:"1",x:10,y:10,z:"t1",type:"test",name:"a",wires:["2"]},
{id:"2",x:10,y:10,z:"t1",type:"subflow:sf1",wires:["3"]},
{id:"3",x:10,y:10,z:"t1",type:"test",foo:"a",wires:[]},
{
id:"sf1",
type:"subflow",
name:"Subflow 2",
info:"",
in:[{wires:[{id:"sf1-1"}]}],
out:[{wires:[{id:"sf1-1",port:0}]}],
status:{wires:[]}
},
{id:"sf1-1",type:"testStatus",name:"test-status-node","z":"sf1",x:166,y:99,"wires":[[]]},
{id:"sn",x:10,y:10,z:"t1",type:"status",foo:"a",wires:[]}
]);
var flow = Flow.create({},config,config.flows["t1"]);
flow.start();
var activeNodes = flow.getActiveNodes();
activeNodes["1"].receive({payload:"test-payload"});
currentNodes["sn"].should.have.a.property("handled",0);
flow.stop().then(function() {
done();
});
});
})
describe("#handleError",function() {
it("passes an error event to the subflow's parent tab catch node - all scope",function(done) {
var config = flowUtils.parseConfig([
{id:"t1",type:"tab"},
{id:"1",x:10,y:10,z:"t1",type:"test",name:"a",wires:["2"]},
{id:"2",x:10,y:10,z:"t1",type:"subflow:sf1",wires:["3"]},
{id:"3",x:10,y:10,z:"t1",type:"test",foo:"a",wires:[]},
{id:"sf1",type:"subflow","name":"Subflow 2","info":"",
"in":[{"wires":[{"id":"sf1-1"}]}],"out":[{"wires":[{"id":"sf1-1","port":0}]}]},
{id:"sf1-1",name:"test-error-node",type:"testError","z":"sf1",x:166,y:99,"wires":[[]]},
{id:"sn",x:10,y:10,z:"t1",type:"catch",foo:"a",wires:[]}
]);
var flow = Flow.create({},config,config.flows["t1"]);
flow.start();
var activeNodes = flow.getActiveNodes();
activeNodes["1"].receive({payload:"test"});
setTimeout(function() {
currentNodes["sn"].should.have.a.property("handled",1);
var statusMessage = currentNodes["sn"].messages[0];
statusMessage.should.have.a.property("error");
statusMessage.error.should.have.a.property("message","test error");
statusMessage.error.should.have.a.property("source");
statusMessage.error.source.should.have.a.property("type","testError");
statusMessage.error.source.should.have.a.property("name","test-error-node");
flow.stop().then(function() {
done();
});
},150);
});
it("passes an error event to the subflow's parent tab catch node - targetted scope",function(done) {
var config = flowUtils.parseConfig([
{id:"t1",type:"tab"},
{id:"1",x:10,y:10,z:"t1",type:"test",name:"a",wires:["2"]},
{id:"2",x:10,y:10,z:"t1",type:"subflow:sf1",wires:["3"]},
{id:"3",x:10,y:10,z:"t1",type:"test",foo:"a",wires:[]},
{id:"sf1",type:"subflow","name":"Subflow 2","info":"",
"in":[{"wires":[{"id":"sf1-1"}]}],"out":[{"wires":[{"id":"sf1-1","port":0}]}]},
{id:"sf1-1",name:"test-error-node",type:"testError","z":"sf1",x:166,y:99,"wires":[[]]},
{id:"sn",x:10,y:10,z:"t1",type:"catch",scope:["2"],wires:[]}
]);
var parentFlowErrorCalled = false;
var flow = Flow.create({handleError:() => { parentFlowErrorCalled = true} },config,config.flows["t1"]);
flow.start();
var activeNodes = flow.getActiveNodes();
activeNodes["1"].receive({payload:"test"});
setTimeout(function() {
parentFlowErrorCalled.should.be.false();
currentNodes["sn"].should.have.a.property("handled",1);
var statusMessage = currentNodes["sn"].messages[0];
statusMessage.should.have.a.property("error");
statusMessage.error.should.have.a.property("message","test error");
statusMessage.error.should.have.a.property("source");
statusMessage.error.source.should.have.a.property("type","testError");
statusMessage.error.source.should.have.a.property("name","test-error-node");
flow.stop().then(function() {
done();
});
},150);
});
});
describe("#env var", function() {
// should be changed according to internal env var representation
function setEnv(node, key, val) {
var flow = node._flow;
if (flow) {
var env = flow.env;
if (!env) {
env = flow.env = {};
}
env[key] = {
name: key,
type: "str",
value: val
};
}
}
it("can access process env var", function(done) {
var config = flowUtils.parseConfig([
{id:"t1",type:"tab"},
{id:"1",x:10,y:10,z:"t1",type:"test",foo:"t1.1",wires:["2"]},
{id:"2",x:10,y:10,z:"t1",type:"subflow:sf1",wires:["3"]},
{id:"3",x:10,y:10,z:"t1",type:"test",foo:"t1.3",wires:[]},
{id:"sf1",type:"subflow",name:"Subflow 2",info:"",
"in":[ {wires:[{id:"sf1-1"}]} ],
"out":[ {wires:[{id:"sf1-2",port:0}]} ]},
{id:"sf1-1",type:"test",z:"sf1",foo:"sf1.1",x:166,y:99,wires:[["sf1-2"]]},
{id:"sf1-2",type:"testEnv",z:"sf1",foo:"sf1-cn",x:166,y:99,wires:[[]]}
]);
var flow = Flow.create({
getSetting: k=> process.env[k],
handleError: (a,b,c) => { console.log(a,b,c); }
},config,config.flows["t1"]);
flow.start();
process.env["__KEY__"] = "__VAL__";
currentNodes["1"].receive({payload: "test"});
setTimeout(function() {
currentNodes["3"].should.have.a.property("received", "__VAL__");
flow.stop().then(function() {
done();
});
},150);
});
it("can access subflow env var", function(done) {
var config = flowUtils.parseConfig([
{id:"t1",type:"tab"},
{id:"1",x:10,y:10,z:"t1",type:"test",foo:"t1.1",wires:["2"]},
{id:"2",x:10,y:10,z:"t1",type:"subflow:sf1",wires:["3"]},
{id:"3",x:10,y:10,z:"t1",type:"test",foo:"t1.3",wires:[]},
{id:"sf1",type:"subflow",name:"Subflow 2",info:"",
"in":[ {wires:[{id:"sf1-1"}]} ],
"out":[ {wires:[{id:"sf1-2",port:0}]} ]},
{id:"sf1-1",type:"test",z:"sf1",foo:"sf1.1",x:166,y:99,wires:[["sf1-2"]]},
{id:"sf1-2",type:"testEnv",z:"sf1",foo:"sf1.2",x:166,y:99,wires:[[]]}
]);
var flow = Flow.create({
getSetting: k=> process.env[k],
handleError: (a,b,c) => { console.log(a,b,c); }
},config,config.flows["t1"]);
flow.start();
var testenv_node = null;
for (var n in currentNodes) {
var node = currentNodes[n];
if (node.type === "testEnv") {
testenv_node = node;
break;
}
}
process.env["__KEY__"] = "__VAL0__";
setEnv(testenv_node, "__KEY__", "__VAL1__");
currentNodes["1"].receive({payload: "test"});
setTimeout(function() {
currentNodes["3"].should.have.a.property("received", "__VAL1__");
flow.stop().then(function() {
done();
});
},150);
});
it("can access nested subflow env var", function(done) {
var config = flowUtils.parseConfig([
{id:"t1",type:"tab"},
{id:"1",x:10,y:10,z:"t1",type:"test",foo:"t1.1",wires:["2"]},
{id:"2",x:10,y:10,z:"t1",type:"subflow:sf1",wires:["3"]},
{id:"3",x:10,y:10,z:"t1",type:"test",foo:"t1.3",wires:[]},
{id:"sf1",type:"subflow",name:"Subflow 1",info:"",
in:[{wires:[{id:"sf1-1"}]}],
out:[{wires:[{id:"sf1-2",port:0}]}]},
{id:"sf2",type:"subflow",name:"Subflow 2",info:"",
in:[{wires:[{id:"sf2-1"}]}],
out:[{wires:[{id:"sf2-2",port:0}]}]},
{id:"sf1-1",type:"test",z:"sf1",foo:"sf1.1",x:166,y:99,wires:[["sf1-2"]]},
{id:"sf1-2",type:"subflow:sf2",z:"sf1",x:166,y:99,wires:[[]]},
{id:"sf2-1",type:"test",z:"sf2",foo:"sf2.1",x:166,y:99,wires:[["sf2-2"]]},
{id:"sf2-2",type:"testEnv",z:"sf2",foo:"sf2.2",x:166,y:99,wires:[[]]},
]);
var flow = Flow.create({
getSetting: k=> process.env[k],
handleError: (a,b,c) => { console.log(a,b,c); }
},config,config.flows["t1"]);
flow.start();
var node_sf1_1 = null;
var node_sf2_1 = null;
var testenv_node = null;
for (var n in currentNodes) {
var node = currentNodes[n];
if (node.foo === "sf1.1") {
node_sf1_1 = node;
}
if (node.foo === "sf2.1") {
node_sf2_1 = node;
}
}
process.env["__KEY__"] = "__VAL0__";
currentNodes["1"].receive({payload: "test"});
setTimeout(function() {
currentNodes["3"].should.have.a.property("received", "__VAL0__");
setEnv(node_sf1_1, "__KEY__", "__VAL1__");
currentNodes["1"].receive({payload: "test"});
setTimeout(function() {
currentNodes["3"].should.have.a.property("received", "__VAL1__");
setEnv(node_sf2_1, "__KEY__", "__VAL2__");
currentNodes["1"].receive({payload: "test"});
setTimeout(function() {
currentNodes["3"].should.have.a.property("received", "__VAL2__");
flow.stop().then(function() {
done();
});
},150);
},150);
},150);
});
});
});

View File

@@ -0,0 +1,617 @@
/**
* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
var should = require("should");
var sinon = require("sinon");
var when = require("when");
var clone = require("clone");
var NR_TEST_UTILS = require("nr-test-utils");
var flows = NR_TEST_UTILS.require("@node-red/runtime/lib/flows");
var RedNode = NR_TEST_UTILS.require("@node-red/runtime/lib/nodes/Node");
var RED = NR_TEST_UTILS.require("@node-red/runtime/lib/nodes");
var events = NR_TEST_UTILS.require("@node-red/runtime/lib/events");
var credentials = NR_TEST_UTILS.require("@node-red/runtime/lib/nodes/credentials");
var typeRegistry = NR_TEST_UTILS.require("@node-red/registry")
var Flow = NR_TEST_UTILS.require("@node-red/runtime/lib/flows/Flow");
describe('flows/index', function() {
var storage;
var eventsOn;
var credentialsClean;
var credentialsLoad;
var flowCreate;
var getType;
var mockLog = {
log: sinon.stub(),
debug: sinon.stub(),
trace: sinon.stub(),
warn: sinon.stub(),
info: sinon.stub(),
metric: sinon.stub(),
_: function() { return "abc"}
}
before(function() {
getType = sinon.stub(typeRegistry,"get",function(type) {
return type.indexOf('missing') === -1;
});
});
after(function() {
getType.restore();
});
beforeEach(function() {
eventsOn = sinon.spy(events,"on");
credentialsClean = sinon.stub(credentials,"clean",function(conf) {
conf.forEach(function(n) {
delete n.credentials;
});
return when.resolve();
});
credentialsLoad = sinon.stub(credentials,"load",function(creds) {
if (creds && creds.hasOwnProperty("$") && creds['$'] === "fail") {
return when.reject("creds error");
}
return when.resolve();
});
flowCreate = sinon.stub(Flow,"create",function(parent, global, flow) {
var id;
if (typeof flow === 'undefined') {
flow = global;
id = '_GLOBAL_';
} else {
id = flow.id;
}
flowCreate.flows[id] = {
flow: flow,
global: global,
start: sinon.spy(),
update: sinon.spy(),
stop: sinon.spy(),
getActiveNodes: function() {
return flow.nodes||{};
},
handleError: sinon.spy(),
handleStatus: sinon.spy()
}
return flowCreate.flows[id];
});
flowCreate.flows = {};
storage = {
saveFlows: function(conf) {
storage.conf = conf;
return when.resolve();
}
}
});
afterEach(function(done) {
eventsOn.restore();
credentialsClean.restore();
credentialsLoad.restore();
flowCreate.restore();
flows.stopFlows().then(done);
});
// describe('#init',function() {
// it('registers the type-registered handler', function() {
// flows.init({},{});
// eventsOn.calledOnce.should.be.true();
// });
// });
describe('#setFlows', function() {
it('sets the full flow', function(done) {
var originalConfig = [
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]},
{id:"t1",type:"tab"}
];
flows.init({log:mockLog, settings:{},storage:storage});
flows.setFlows(originalConfig).then(function() {
credentialsClean.called.should.be.true();
storage.hasOwnProperty('conf').should.be.true();
flows.getFlows().flows.should.eql(originalConfig);
done();
});
});
it('loads the full flow for type load', function(done) {
var originalConfig = [
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]},
{id:"t1",type:"tab"}
];
var loadStorage = {
saveFlows: function(conf) {
loadStorage.conf = conf;
return when.resolve(456);
},
getFlows: function() {
return when.resolve({flows:originalConfig,rev:123})
}
}
flows.init({log:mockLog, settings:{},storage:loadStorage});
flows.setFlows(originalConfig,"load").then(function() {
credentialsClean.called.should.be.false();
// 'load' type does not trigger a save
loadStorage.hasOwnProperty('conf').should.be.false();
flows.getFlows().flows.should.eql(originalConfig);
done();
});
});
it('extracts credentials from the full flow', function(done) {
var originalConfig = [
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[],credentials:{"a":1}},
{id:"t1",type:"tab"}
];
flows.init({log:mockLog, settings:{},storage:storage});
flows.setFlows(originalConfig).then(function() {
credentialsClean.called.should.be.true();
storage.hasOwnProperty('conf').should.be.true();
var cleanedFlows = flows.getFlows();
storage.conf.flows.should.eql(cleanedFlows.flows);
cleanedFlows.flows.should.not.eql(originalConfig);
cleanedFlows.flows[0].credentials = {"a":1};
cleanedFlows.flows.should.eql(originalConfig);
done();
});
});
it('sets the full flow including credentials', function(done) {
var originalConfig = [
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]},
{id:"t1",type:"tab"}
];
var credentials = {"t1-1":{"a":1}};
flows.init({log:mockLog, settings:{},storage:storage});
flows.setFlows(originalConfig,credentials).then(function() {
credentialsClean.called.should.be.false();
credentialsLoad.called.should.be.true();
credentialsLoad.lastCall.args[0].should.eql(credentials);
flows.getFlows().flows.should.eql(originalConfig);
done();
});
});
it('updates existing flows with partial deployment - nodes', function(done) {
var originalConfig = [
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]},
{id:"t1",type:"tab"}
];
var newConfig = clone(originalConfig);
newConfig.push({id:"t1-2",x:10,y:10,z:"t1",type:"test",wires:[]});
newConfig.push({id:"t2",type:"tab"});
newConfig.push({id:"t2-1",x:10,y:10,z:"t2",type:"test",wires:[]});
storage.getFlows = function() {
return when.resolve({flows:originalConfig});
}
events.once('nodes-started',function() {
flows.setFlows(newConfig,"nodes").then(function() {
flows.getFlows().flows.should.eql(newConfig);
flowCreate.flows['t1'].update.called.should.be.true();
flowCreate.flows['t2'].start.called.should.be.true();
flowCreate.flows['_GLOBAL_'].update.called.should.be.true();
done();
})
});
flows.init({log:mockLog, settings:{},storage:storage});
flows.load().then(function() {
flows.startFlows();
});
});
it('updates existing flows with partial deployment - flows', function(done) {
var originalConfig = [
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]},
{id:"t1",type:"tab"}
];
var newConfig = clone(originalConfig);
newConfig.push({id:"t1-2",x:10,y:10,z:"t1",type:"test",wires:[]});
newConfig.push({id:"t2",type:"tab"});
newConfig.push({id:"t2-1",x:10,y:10,z:"t2",type:"test",wires:[]});
storage.getFlows = function() {
return when.resolve({flows:originalConfig});
}
events.once('nodes-started',function() {
flows.setFlows(newConfig,"nodes").then(function() {
flows.getFlows().flows.should.eql(newConfig);
flowCreate.flows['t1'].update.called.should.be.true();
flowCreate.flows['t2'].start.called.should.be.true();
flowCreate.flows['_GLOBAL_'].update.called.should.be.true();
flows.stopFlows().then(done);
})
});
flows.init({log:mockLog, settings:{},storage:storage});
flows.load().then(function() {
flows.startFlows();
});
});
it('returns error if it cannot decrypt credentials', function(done) {
var originalConfig = [
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]},
{id:"t1",type:"tab"}
];
var credentials = {"$":"fail"};
flows.init({log:mockLog, settings:{},storage:storage});
flows.setFlows(originalConfig,credentials).then(function() {
done("Unexpected success when credentials couldn't be decrypted")
}).catch(function(err) {
done();
});
});
});
describe('#load', function() {
it('loads the flow config', function(done) {
var originalConfig = [
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]},
{id:"t1",type:"tab"}
];
storage.getFlows = function() {
return when.resolve({flows:originalConfig});
}
flows.init({log:mockLog, settings:{},storage:storage});
flows.load().then(function() {
credentialsLoad.called.should.be.true();
// 'load' type does not trigger a save
storage.hasOwnProperty('conf').should.be.false();
flows.getFlows().flows.should.eql(originalConfig);
done();
});
});
});
describe('#startFlows', function() {
it('starts the loaded config', function(done) {
var originalConfig = [
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]},
{id:"t1",type:"tab"}
];
storage.getFlows = function() {
return when.resolve({flows:originalConfig});
}
events.once('nodes-started',function() {
Object.keys(flowCreate.flows).should.eql(['_GLOBAL_','t1']);
done();
});
flows.init({log:mockLog, settings:{},storage:storage});
flows.load().then(function() {
flows.startFlows();
});
});
it('does not start if nodes missing', function(done) {
var originalConfig = [
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]},
{id:"t1-2",x:10,y:10,z:"t1",type:"missing",wires:[]},
{id:"t1",type:"tab"}
];
storage.getFlows = function() {
return when.resolve({flows:originalConfig});
}
flows.init({log:mockLog, settings:{},storage:storage});
flows.load().then(function() {
flows.startFlows();
flowCreate.called.should.be.false();
done();
});
});
it('starts when missing nodes registered', function(done) {
var originalConfig = [
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]},
{id:"t1-2",x:10,y:10,z:"t1",type:"missing",wires:[]},
{id:"t1-3",x:10,y:10,z:"t1",type:"missing2",wires:[]},
{id:"t1",type:"tab"}
];
storage.getFlows = function() {
return when.resolve({flows:originalConfig});
}
flows.init({log:mockLog, settings:{},storage:storage});
flows.load().then(function() {
flows.startFlows();
flowCreate.called.should.be.false();
events.emit("type-registered","missing");
setTimeout(function() {
flowCreate.called.should.be.false();
events.emit("type-registered","missing2");
setTimeout(function() {
flowCreate.called.should.be.true();
done();
},10);
},10);
});
});
});
describe.skip('#get',function() {
});
describe('#eachNode', function() {
it('iterates the flow nodes', function(done) {
var originalConfig = [
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]},
{id:"t1",type:"tab"}
];
storage.getFlows = function() {
return when.resolve({flows:originalConfig});
}
flows.init({log:mockLog, settings:{},storage:storage});
flows.load().then(function() {
var c = 0;
flows.eachNode(function(node) {
c++
})
c.should.equal(2);
done();
});
});
});
describe('#stopFlows', function() {
});
// describe('#handleError', function() {
// it('passes error to correct flow', function(done) {
// var originalConfig = [
// {id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]},
// {id:"t1",type:"tab"}
// ];
// storage.getFlows = function() {
// return when.resolve({flows:originalConfig});
// }
//
// events.once('nodes-started',function() {
// flows.handleError(originalConfig[0],"message",{});
// flowCreate.flows['t1'].handleError.called.should.be.true();
// done();
// });
//
// flows.init({log:mockLog, settings:{},storage:storage});
// flows.load().then(function() {
// flows.startFlows();
// });
// });
// it('passes error to flows that use the originating global config', function(done) {
// var originalConfig = [
// {id:"configNode",type:"test"},
// {id:"t1",type:"tab"},
// {id:"t1-1",x:10,y:10,z:"t1",type:"test",config:"configNode",wires:[]},
// {id:"t2",type:"tab"},
// {id:"t2-1",x:10,y:10,z:"t2",type:"test",wires:[]},
// {id:"t3",type:"tab"},
// {id:"t3-1",x:10,y:10,z:"t3",type:"test",config:"configNode",wires:[]}
// ];
// storage.getFlows = function() {
// return when.resolve({flows:originalConfig});
// }
//
// events.once('nodes-started',function() {
// flows.handleError(originalConfig[0],"message",{});
// try {
// flowCreate.flows['t1'].handleError.called.should.be.true();
// flowCreate.flows['t2'].handleError.called.should.be.false();
// flowCreate.flows['t3'].handleError.called.should.be.true();
// done();
// } catch(err) {
// done(err);
// }
// });
//
// flows.init({log:mockLog, settings:{},storage:storage});
// flows.load().then(function() {
// flows.startFlows();
// });
// });
// });
// describe('#handleStatus', function() {
// it('passes status to correct flow', function(done) {
// var originalConfig = [
// {id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]},
// {id:"t1",type:"tab"}
// ];
// storage.getFlows = function() {
// return when.resolve({flows:originalConfig});
// }
//
// events.once('nodes-started',function() {
// flows.handleStatus(originalConfig[0],"message");
// flowCreate.flows['t1'].handleStatus.called.should.be.true();
// done();
// });
//
// flows.init({log:mockLog, settings:{},storage:storage});
// flows.load().then(function() {
// flows.startFlows();
// });
// });
//
// it('passes status to flows that use the originating global config', function(done) {
// var originalConfig = [
// {id:"configNode",type:"test"},
// {id:"t1",type:"tab"},
// {id:"t1-1",x:10,y:10,z:"t1",type:"test",config:"configNode",wires:[]},
// {id:"t2",type:"tab"},
// {id:"t2-1",x:10,y:10,z:"t2",type:"test",wires:[]},
// {id:"t3",type:"tab"},
// {id:"t3-1",x:10,y:10,z:"t3",type:"test",config:"configNode",wires:[]}
// ];
// storage.getFlows = function() {
// return when.resolve({flows:originalConfig});
// }
//
// events.once('nodes-started',function() {
// flows.handleStatus(originalConfig[0],"message");
// try {
// flowCreate.flows['t1'].handleStatus.called.should.be.true();
// flowCreate.flows['t2'].handleStatus.called.should.be.false();
// flowCreate.flows['t3'].handleStatus.called.should.be.true();
// done();
// } catch(err) {
// done(err);
// }
// });
//
// flows.init({log:mockLog, settings:{},storage:storage});
// flows.load().then(function() {
// flows.startFlows();
// });
// });
// });
describe('#checkTypeInUse', function() {
before(function() {
sinon.stub(typeRegistry,"getNodeInfo",function(id) {
if (id === 'unused-module') {
return {types:['one','two','three']}
} else {
return {types:['one','test','three']}
}
});
});
after(function() {
typeRegistry.getNodeInfo.restore();
});
it('returns cleanly if type not is use', function(done) {
var originalConfig = [
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]},
{id:"t1",type:"tab"}
];
flows.init({log:mockLog, settings:{},storage:storage});
flows.setFlows(originalConfig).then(function() {
flows.checkTypeInUse("unused-module");
done();
});
});
it('throws error if type is in use', function(done) {
var originalConfig = [
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]},
{id:"t1",type:"tab"}
];
flows.init({log:mockLog, settings:{},storage:storage});
flows.setFlows(originalConfig).then(function() {
/*jshint immed: false */
try {
flows.checkTypeInUse("used-module");
done("type_in_use error not thrown");
} catch(err) {
err.code.should.eql("type_in_use");
done();
}
});
});
});
describe('#addFlow', function() {
it("rejects duplicate node id",function(done) {
var originalConfig = [
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]},
{id:"t1",type:"tab"}
];
storage.getFlows = function() {
return when.resolve({flows:originalConfig});
}
flows.init({log:mockLog, settings:{},storage:storage});
flows.load().then(function() {
flows.addFlow({
label:'new flow',
nodes:[
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]}
]
}).then(function() {
done(new Error('failed to reject duplicate node id'));
}).catch(function(err) {
done();
})
});
});
it("addFlow",function(done) {
var originalConfig = [
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]},
{id:"t1",type:"tab"}
];
storage.getFlows = function() {
return when.resolve({flows:originalConfig});
}
storage.setFlows = function() {
return when.resolve();
}
flows.init({log:mockLog, settings:{},storage:storage});
flows.load().then(function() {
return flows.startFlows();
}).then(function() {
flows.addFlow({
label:'new flow',
nodes:[
{id:"t2-1",x:10,y:10,z:"t1",type:"test",wires:[]},
{id:"t2-2",x:10,y:10,z:"t1",type:"test",wires:[]},
{id:"t2-3",z:"t1",type:"test"}
]
}).then(function(id) {
flows.getFlows().flows.should.have.lengthOf(6);
var createdFlows = Object.keys(flowCreate.flows);
createdFlows.should.have.lengthOf(3);
createdFlows[2].should.eql(id);
done();
}).catch(function(err) {
done(err);
})
});
});
})
describe('#updateFlow', function() {
it.skip("updateFlow");
})
describe('#removeFlow', function() {
it.skip("removeFlow");
})
describe('#disableFlow', function() {
it.skip("disableFlow");
})
describe('#enableFlow', function() {
it.skip("enableFlow");
})
});

View File

@@ -0,0 +1,802 @@
/**
* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
var should = require("should");
var sinon = require("sinon");
var when = require("when");
var clone = require("clone");
var NR_TEST_UTILS = require("nr-test-utils");
var flowUtil = NR_TEST_UTILS.require("@node-red/runtime/lib/flows/util");
var typeRegistry = NR_TEST_UTILS.require("@node-red/registry");
var redUtil = NR_TEST_UTILS.require("@node-red/util").util;
describe('flows/util', function() {
var getType;
before(function() {
getType = sinon.stub(typeRegistry,"get",function(type) {
return type!=='missing';
});
});
after(function() {
getType.restore();
});
describe('#mapEnvVarProperties',function() {
before(function() {
process.env.foo1 = "bar1";
process.env.foo2 = "bar2";
process.env.foo3 = "bar3";
})
after(function() {
delete process.env.foo1;
delete process.env.foo2;
delete process.env.foo3;
})
it('handles ENV substitutions in an object - $()', function() {
var foo = {a:"$(foo1)",b:"$(foo2)",c:{d:"$(foo3)"}};
for (var p in foo) {
if (foo.hasOwnProperty(p)) {
flowUtil.mapEnvVarProperties(foo,p,{getSetting: p => process.env[p]});
}
}
foo.should.eql({ a: 'bar1', b: 'bar2', c: { d: 'bar3' } } );
});
it('handles ENV substitutions in an object - ${}', function() {
var foo = {a:"${foo1}",b:"${foo2}",c:{d:"${foo3}"}};
for (var p in foo) {
if (foo.hasOwnProperty(p)) {
flowUtil.mapEnvVarProperties(foo,p,{getSetting: p => process.env[p]});
}
}
foo.should.eql({ a: 'bar1', b: 'bar2', c: { d: 'bar3' } } );
});
it('gets ENV from parent flow', function() {
var foo = {a:"$(unknown)",b:"$(foo2)",c:{d:"$(foo3)"}};
for (var p in foo) {
if (foo.hasOwnProperty(p)) {
flowUtil.mapEnvVarProperties(foo,p,{
getSetting: name => name[0]==='f'?name.toUpperCase():undefined
});
}
}
foo.should.eql({ a: '$(unknown)', b: 'FOO2', c: { d: 'FOO3' } } );
});
});
describe('#getEnvVar',function() {
before(function() {
process.env.foo1 = "bar1";
})
after(function() {
delete process.env.foo1;
})
it('returns a known env var', function() {
flowUtil.init({settings:{}});
flowUtil.getEnvVar("foo1").should.equal("bar1")
})
it('returns undefined for an unknown env var', function() {
flowUtil.init({settings:{}});
(flowUtil.getEnvVar("foo2") === undefined).should.be.true()
})
it('returns undefined for an excluded env var', function() {
flowUtil.init({settings:{envVarExcludes:['foo1']}});
(flowUtil.getEnvVar("foo1") === undefined).should.be.true()
})
});
describe('#diffNodes',function() {
it('handles a null old node', function() {
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();
});
it('ignores wiring changes', function() {
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();
});
it('spots existing property change - number', function() {
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();
});
it('spots existing property change - truthy', function() {
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();
});
it('spots existing property change - array', function() {
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();
});
it('spots added property', function() {
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();
});
});
describe('#parseConfig',function() {
it('parses a single-tab flow', function() {
var originalConfig = [
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]},
{id:"t1",type:"tab"}
];
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":[]}}}},"groups":{},"missingTypes":[]};
parsedConfig.should.eql(expectedConfig);
});
it('parses a single-tab flow with global config node', function() {
var originalConfig = [
{id:"t1-1",x:10,y:10,z:"t1",type:"test",foo:"cn", wires:[]},
{id:"cn",type:"test"},
{id:"t1",type:"tab"}
];
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":[]}}}},"groups":{},"missingTypes":[]};
parsedConfig.should.eql(expectedConfig);
});
it('parses a multi-tab flow', function() {
var originalConfig = [
{id:"t1",type:"tab"},
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]},
{id:"t2",type:"tab"},
{id:"t2-1",x:10,y:10,z:"t2",type:"test",wires:[]}
];
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":[]}}}},"groups":{},"missingTypes":[]};
parsedConfig.should.eql(expectedConfig);
});
it('parses a subflow flow', function() {
var originalConfig = [
{id:"t1",type:"tab"},
{id:"t1-1",x:10,y:10,z:"t1",type:"subflow:sf1",wires:[]},
{id:"sf1",type:"subflow"},
{id:"sf1-1",x:10,y:10,z:"sf1",type:"test",wires:[]}
];
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"}}}},"groups":{},"missingTypes":[]};
parsedConfig.should.eql(expectedConfig);
});
it('parses a flow with a missing type', function() {
var originalConfig = [
{id:"t1",type:"tab"},
{id:"t1-1",x:10,y:10,z:"t1",type:"sf1",wires:[]},
{id:"t1-2",x:10,y:10,z:"t1",type:"missing",wires:[]},
];
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: [] }}}},"groups":{},"missingTypes":["missing"]};
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":[]}}}},"groups":{},"missingTypes":[]};
parsedConfig.should.eql(expectedConfig);
});
it('parses a flow including a group', function() {
var originalConfig = [
{id:"t1",type:"tab"},
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]},
{id:"g1",type:"group",z:"t1"}
];
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":[]},"g1":{"id":"g1","type":"group","z":"t1"}},"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":[]}}}},"groups":{"g1":{"id":"g1","type":"group","z":"t1"}},"missingTypes":[]}
parsedConfig.should.eql(expectedConfig);
});
});
describe('#diffConfigs', function() {
it('handles an identical configuration', function() {
var config = [{id:"123",type:"test",foo:"a",wires:[]}];
var originalConfig = flowUtil.parseConfig(clone(config));
var changedConfig = flowUtil.parseConfig(clone(config));
originalConfig.missingTypes.should.have.length(0);
var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);
diffResult.added.should.have.length(0);
diffResult.changed.should.have.length(0);
diffResult.removed.should.have.length(0);
diffResult.rewired.should.have.length(0);
diffResult.linked.should.have.length(0);
});
it('identifies nodes with changed properties, including downstream linked', function() {
var config = [{id:"1",type:"test",foo:"a",wires:[]},{id:"2",type:"test",bar:"b",wires:[[1]]},{id:"3",type:"test",foo:"a",wires:[]}];
var newConfig = clone(config);
newConfig[0].foo = "b";
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.should.eql(["1"]);
diffResult.removed.should.have.length(0);
diffResult.rewired.should.have.length(0);
diffResult.linked.should.eql(["2"]);
});
it('identifies nodes with changed properties, including upstream linked', function() {
var config = [{id:"1",type:"test",foo:"a",wires:[]},{id:"2",type:"test",bar:"b",wires:[["1"]]},{id:"3",type:"test",foo:"a",wires:[]}];
var newConfig = clone(config);
newConfig[1].bar = "c";
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.should.eql(["2"]);
diffResult.removed.should.have.length(0);
diffResult.rewired.should.have.length(0);
diffResult.linked.should.eql(["1"]);
});
it('identifies nodes with changed credentials, including downstream linked', function() {
var config = [{id:"1",type:"test",wires:[]},{id:"2",type:"test",bar:"b",wires:[["1"]]},{id:"3",type:"test",foo:"a",wires:[]}];
var newConfig = clone(config);
newConfig[0].credentials = {};
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.should.eql(["1"]);
diffResult.removed.should.have.length(0);
diffResult.rewired.should.have.length(0);
diffResult.linked.should.eql(["2"]);
});
it('identifies nodes with changed wiring', function() {
var config = [{id:"1",type:"test",foo:"a",wires:[]},{id:"2",type:"test",bar:"b",wires:[["1"]]},{id:"3",type:"test",foo:"a",wires:[]}];
var newConfig = clone(config);
newConfig[1].wires[0][0] = "3";
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.should.have.length(0);
diffResult.removed.should.have.length(0);
diffResult.rewired.should.eql(["2"]);
diffResult.linked.sort().should.eql(["1","3"]);
});
it('identifies nodes with changed wiring - second connection added', function() {
var config = [{id:"1",type:"test",foo:"a",wires:[]},{id:"2",type:"test",bar:"b",wires:[["1"]]},{id:"3",type:"test",foo:"a",wires:[]}];
var newConfig = clone(config);
newConfig[1].wires[0].push("1");
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.should.have.length(0);
diffResult.removed.should.have.length(0);
diffResult.rewired.should.eql(["2"]);
diffResult.linked.sort().should.eql(["1"]);
});
it('identifies nodes with changed wiring - node connected', function() {
var config = [{id:"1",type:"test",foo:"a",wires:[["2"]]},{id:"2",type:"test",bar:"b",wires:[[]]},{id:"3",type:"test",foo:"a",wires:[]}];
var newConfig = clone(config);
newConfig[1].wires.push("3");
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.should.have.length(0);
diffResult.removed.should.have.length(0);
diffResult.rewired.should.eql(["2"]);
diffResult.linked.sort().should.eql(["1","3"]);
});
it('identifies new nodes', function() {
var config = [{id:"1",type:"test",foo:"a",wires:[]},{id:"3",type:"test",foo:"a",wires:[]}];
var newConfig = clone(config);
newConfig.push({id:"2",type:"test",bar:"b",wires:[["1"]]});
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.eql(["2"]);
diffResult.changed.should.have.length(0);
diffResult.removed.should.have.length(0);
diffResult.rewired.should.have.length(0);
diffResult.linked.sort().should.eql(["1"]);
});
it('identifies deleted nodes', function() {
var config = [{id:"1",type:"test",foo:"a",wires:[["2"]]},{id:"2",type:"test",bar:"b",wires:[["3"]]},{id:"3",type:"test",foo:"a",wires:[]}];
var newConfig = clone(config);
newConfig.splice(1,1);
newConfig[0].wires = [];
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.should.have.length(0);
diffResult.removed.should.eql(["2"]);
diffResult.rewired.should.eql(["1"]);
diffResult.linked.sort().should.eql(["3"]);
});
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"]]},
{id:"3",type:"test",foo:"a",wires:[]},
{id:"configNode",type:"testConfig"}
];
var newConfig = clone(config);
newConfig[3].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","configNode"]);
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() {
var config = [
{id:"1",type:"test",wires:[["2"]]},
{id:"2",type:"subflow:sf1",wires:[["3"]]},
{id:"3",type:"test",wires:[]},
{id:"sf1",type:"subflow"},
{id:"sf1-1",z:"sf1",type:"test",foo:"a",wires:[]},
{id:"4",type:"subflow:sf1",wires:[]}
];
var newConfig = clone(config);
newConfig[4].foo = "b";
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(['2', '4', 'sf1']);
diffResult.removed.should.have.length(0);
diffResult.rewired.should.have.length(0);
diffResult.linked.sort().should.eql(["1","3"]);
});
it('marks a parent subflow as changed for an internal wiring change', function() {
var config = [
{id:"1",type:"test",wires:[["2"]]},
{id:"2",type:"subflow:sf1",wires:[["3"]]},
{id:"3",type:"test",wires:[]},
{id:"sf1",type:"subflow"},
{id:"sf1-1",z:"sf1",type:"test",wires:[]},
{id:"sf1-2",z:"sf1",type:"test",wires:[]}
];
var newConfig = clone(config);
newConfig[4].wires = [["sf1-2"]];
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(['2', 'sf1']);
diffResult.removed.should.have.length(0);
diffResult.rewired.should.have.length(0);
diffResult.linked.sort().should.eql(["1","3"]);
});
it('marks a parent subflow as changed for an internal node add', function() {
var config = [
{id:"1",type:"test",wires:[["2"]]},
{id:"2",type:"subflow:sf1",wires:[["3"]]},
{id:"3",type:"test",wires:[]},
{id:"sf1",type:"subflow"},
{id:"sf1-1",z:"sf1",type:"test",wires:[]},
{id:"sf1-2",z:"sf1",type:"test",wires:[]}
];
var newConfig = clone(config);
newConfig.push({id:"sf1-3",z:"sf1",type:"test",wires:[]});
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(['2', 'sf1']);
diffResult.removed.should.have.length(0);
diffResult.rewired.should.have.length(0);
diffResult.linked.sort().should.eql(["1","3"]);
});
it('marks a parent subflow as changed for an internal node delete', function() {
var config = [
{id:"1",type:"test",wires:[["2"]]},
{id:"2",type:"subflow:sf1",wires:[["3"]]},
{id:"3",type:"test",wires:[]},
{id:"sf1",type:"subflow"},
{id:"sf1-1",z:"sf1",type:"test",wires:[]},
{id:"sf1-2",z:"sf1",type:"test",wires:[]}
];
var newConfig = clone(config);
newConfig.splice(5,1);
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(['2', 'sf1']);
diffResult.removed.should.have.length(1);
diffResult.removed.sort().should.eql(['sf1-2']);
diffResult.rewired.should.have.length(0);
diffResult.linked.sort().should.eql(["1","3"]);
});
it('marks a parent subflow as changed for an internal subflow wiring change - input removed', function() {
var config = [
{id:"1",type:"test",wires:[["2"]]},
{id:"2",type:"subflow:sf1",wires:[["3"]]},
{id:"3",type:"test",wires:[]},
{id:"sf1",type:"subflow","in": [{"wires": [{"id": "sf1-1"}]}],"out": [{"wires": [{"id": "sf1-2","port": 0}]}]},
{id:"sf1-1",z:"sf1",type:"test",wires:[]},
{id:"sf1-2",z:"sf1",type:"test",wires:[]}
];
var newConfig = clone(config);
newConfig[3].in[0].wires = [];
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(['2', 'sf1']);
diffResult.removed.should.have.length(0);
diffResult.rewired.should.have.length(0);
diffResult.linked.sort().should.eql(["1","3"]);
});
it('marks a parent subflow as changed for an internal subflow wiring change - input added', function() {
var config = [
{id:"1",type:"test",wires:[["2"]]},
{id:"2",type:"subflow:sf1",wires:[["3"]]},
{id:"3",type:"test",wires:[]},
{id:"sf1",type:"subflow","in": [{"wires": [{"id": "sf1-1"}]}],"out": [{"wires": [{"id": "sf1-2","port": 0}]}]},
{id:"sf1-1",z:"sf1",type:"test",wires:[]},
{id:"sf1-2",z:"sf1",type:"test",wires:[]}
];
var newConfig = clone(config);
newConfig[3].in[0].wires.push({"id":"sf1-2"});
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(['2', 'sf1']);
diffResult.removed.should.have.length(0);
diffResult.rewired.should.have.length(0);
diffResult.linked.sort().should.eql(["1","3"]);
});
it('marks a parent subflow as changed for an internal subflow wiring change - output added', function() {
var config = [
{id:"1",type:"test",wires:[["2"]]},
{id:"2",type:"subflow:sf1",wires:[["3"]]},
{id:"3",type:"test",wires:[]},
{id:"sf1",type:"subflow","in": [{"wires": [{"id": "sf1-1"}]}],"out": [{"wires": [{"id": "sf1-2","port": 0}]}]},
{id:"sf1-1",z:"sf1",type:"test",wires:[]},
{id:"sf1-2",z:"sf1",type:"test",wires:[]}
];
var newConfig = clone(config);
newConfig[3].out[0].wires.push({"id":"sf1-2","port":0});
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(['2', 'sf1']);
diffResult.removed.should.have.length(0);
diffResult.rewired.should.have.length(0);
diffResult.linked.sort().should.eql(["1","3"]);
});
it('marks a parent subflow as changed for an internal subflow wiring change - output removed', function() {
var config = [
{id:"1",type:"test",wires:[["2"]]},
{id:"2",type:"subflow:sf1",wires:[["3"]]},
{id:"3",type:"test",wires:[]},
{id:"sf1",type:"subflow","in": [{"wires": [{"id": "sf1-1"}]}],"out": [{"wires": [{"id": "sf1-2","port": 0}]}]},
{id:"sf1-1",z:"sf1",type:"test",wires:[]},
{id:"sf1-2",z:"sf1",type:"test",wires:[]}
];
var newConfig = clone(config);
newConfig[3].out[0].wires = [];
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(['2', 'sf1']);
diffResult.removed.should.have.length(0);
diffResult.rewired.should.have.length(0);
diffResult.linked.sort().should.eql(["1","3"]);
});
it('marks a parent subflow as changed for a global config node change', function() {
var config = [
{id:"1",type:"test",wires:[["2"]]},
{id:"2",type:"subflow:sf1",wires:[["3"]]},
{id:"3",type:"test",wires:[]},
{id:"sf1",type:"subflow"},
{id:"sf1-1",z:"sf1",prop:"configNode",type:"test",wires:[]},
{id:"sf1-2",z:"sf1",type:"test",wires:[]},
{id:"configNode",a:"foo",type:"test",wires:[]}
];
var newConfig = clone(config);
newConfig[6].a = "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(['2', "configNode", 'sf1']);
diffResult.removed.should.have.length(0);
diffResult.rewired.should.have.length(0);
diffResult.linked.sort().should.eql(["1","3"]);
});
it('marks a parent subflow as changed for an internal subflow instance change', function() {
var config = [
{id:"1",type:"test",wires:[["2"]]},
{id:"2",type:"subflow:sf1",wires:[["3"]]},
{id:"3",type:"test",wires:[]},
{id:"sf1",type:"subflow"},
{id:"sf2",type:"subflow"},
{id:"sf1-1",z:"sf1",type:"test",wires:[]},
{id:"sf1-2",z:"sf1",type:"subflow:sf2",wires:[]},
{id:"sf2-1",z:"sf2",type:"test",wires:[]},
{id:"sf2-2",z:"sf2",type:"test",wires:[]},
];
var newConfig = clone(config);
newConfig[8].a = "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(['2', 'sf1', 'sf2']);
diffResult.removed.should.have.length(0);
diffResult.rewired.should.have.length(0);
diffResult.linked.sort().should.eql(["1","3"]);
});
it('ignores tab changes that are immaterial', function() {
var config = [{id:"1",type:"tab",label:"fred"},{id:"2",type:"test",bar:"b",wires:[["1"]],z:"1"}];
var newConfig = clone(config);
newConfig[0].label = "barney";
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.should.have.length(0);
diffResult.removed.should.have.length(0);
diffResult.rewired.should.have.length(0);
});
it('marks a deleted tab as removed', function() {
var config = [{id:"f1",type:"tab",label:"fred"},{id:"n1",type:"test",bar:"b",wires:[["1"]],z:"f1"},
{id:"f2",type:"tab",label:"fred"},{id:"n2",type:"test",bar:"b",wires:[["1"]],z:"f2"}];
var newConfig = clone(config);
newConfig = newConfig.slice(0,2);
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.should.have.length(0);
diffResult.removed.sort().should.eql(['f2', 'n2']);
diffResult.rewired.should.have.length(0);
});
it('marks all nodes as added when tab state changes disabled to enabled', function() {
var config = [{id:"1",type:"tab",disabled:true,label:"fred"},{id:"2",type:"test",bar:"b",wires:[["1"]],z:"1"},{id:"3",type:"test"}];
var newConfig = clone(config);
newConfig[0].disabled = false;
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(2);
diffResult.added.sort().should.eql(["1","2"]);
diffResult.changed.should.have.length(0);
diffResult.removed.should.have.length(0);
diffResult.rewired.should.have.length(0);
});
it('marks all nodes as removed when tab state changes enabled to disabled', function() {
var config = [{id:"1",type:"tab",disabled:false,label:"fred"},{id:"2",type:"test",bar:"b",wires:[["1"]],z:"1"},{id:"3",type:"test"}];
var newConfig = clone(config);
newConfig[0].disabled = true;
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.should.have.length(0);
diffResult.removed.should.have.length(2);
diffResult.removed.sort().should.eql(["1","2"]);
diffResult.rewired.should.have.length(0);
});
it('marks a node as removed when its state changes enabled to disabled', function() {
var config = [{id:"1",type:"tab",disabled:false,label:"fred"},{id:"2",type:"test",bar:"b",wires:[["1"]],z:"1"},{id:"3",type:"test"}];
var newConfig = clone(config);
newConfig[1].d = true;
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.should.have.length(2);
diffResult.changed.sort().should.eql(["1","2"]);
diffResult.removed.should.have.length(1);
diffResult.removed.sort().should.eql(["2"]);
diffResult.rewired.should.have.length(0);
});
});
});