diff --git a/test/nodes/core/logic/17-split_spec.js b/test/nodes/core/logic/17-split_spec.js index dfc57fc3b..9fed82cd4 100644 --- a/test/nodes/core/logic/17-split_spec.js +++ b/test/nodes/core/logic/17-split_spec.js @@ -20,6 +20,8 @@ var joinNode = require("../../../../nodes/core/logic/17-split.js"); var helper = require("node-red-node-test-helper"); var RED = require("../../../../red/red.js"); +var TimeoutForErrorCase = 20; + describe('SPLIT node', function() { before(function(done) { @@ -264,6 +266,126 @@ describe('SPLIT node', function() { }); }); + it('should handle invalid spltType (not an array)', function (done) { + var flow = [{ id: "sn1", type: "split", splt: "1", spltType: "bin", wires: [["sn2"]] }, + { id: "sn2", type: "helper" }]; + helper.load(splitNode, flow, function () { + var sn1 = helper.getNode("sn1"); + var sn2 = helper.getNode("sn2"); + setTimeout(function () { + done(); + }, TimeoutForErrorCase); + sn2.on("input", function (msg) { + done(new Error("This path does not go through.")); + }); + sn1.receive({ payload: "123" }); + }); + }); + + it('should handle invalid splt length', function (done) { + var flow = [{ id: "sn1", type: "split", splt: 0, spltType: "len", wires: [["sn2"]] }, + { id: "sn2", type: "helper" }]; + helper.load(splitNode, flow, function () { + var sn1 = helper.getNode("sn1"); + var sn2 = helper.getNode("sn2"); + setTimeout(function () { + done(); + }, TimeoutForErrorCase); + sn2.on("input", function (msg) { + done(new Error("This path does not go through.")); + }); + sn1.receive({ payload: "123" }); + }); + }); + + it('should handle invalid array splt length', function (done) { + var flow = [{ id: "sn1", type: "split", arraySplt: 0, arraySpltType: "len", wires: [["sn2"]] }, + { id: "sn2", type: "helper" }]; + helper.load(splitNode, flow, function () { + var sn1 = helper.getNode("sn1"); + var sn2 = helper.getNode("sn2"); + setTimeout(function () { + done(); + }, TimeoutForErrorCase); + sn2.on("input", function (msg) { + done(new Error("This path does not go through.")); + }); + sn1.receive({ payload: "123" }); + }); + }); + + it('should ceil count value when msg.payload type is string', function (done) { + var flow = [{ id: "sn1", type: "split", splt: "2", spltType: "len", wires: [["sn2"]] }, + { id: "sn2", type: "helper" }]; + helper.load(splitNode, flow, function () { + var sn1 = helper.getNode("sn1"); + var sn2 = helper.getNode("sn2"); + sn2.on("input", function (msg) { + msg.should.have.property("parts"); + msg.parts.should.have.property("count", 2); + msg.parts.should.have.property("index"); + if (msg.parts.index === 0) { msg.payload.length.should.equal(2); } + if (msg.parts.index === 1) { msg.payload.length.should.equal(1); done(); } + }); + sn1.receive({ payload: "123" }); + }); + }); + + it('should handle spltBufferString value of undefined', function (done) { + var flow = [{ id: "sn1", type: "split", wires: [["sn2"]], splt: "[52]", spltType: "bin" }, + { id: "sn2", type: "helper" }]; + helper.load(splitNode, flow, function () { + var sn1 = helper.getNode("sn1"); + var sn2 = helper.getNode("sn2"); + sn2.on("input", function (msg) { + try { + msg.should.have.property("parts"); + msg.parts.should.have.property("index"); + if (msg.parts.index === 0) { msg.payload.toString().should.equal("123"); done(); } + } catch (err) { + done(err); + } + }); + sn1.receive({ payload: "123" }); + }); + }); + + it('should ceil count value when msg.payload type is Buffer', function (done) { + var flow = [{ id: "sn1", type: "split", splt: "2", spltType: "len", wires: [["sn2"]] }, + { id: "sn2", type: "helper" }]; + helper.load(splitNode, flow, function () { + var sn1 = helper.getNode("sn1"); + var sn2 = helper.getNode("sn2"); + sn2.on("input", function (msg) { + msg.should.have.property("parts"); + msg.parts.should.have.property("count", 2); + msg.parts.should.have.property("index"); + if (msg.parts.index === 0) { msg.payload.length.should.equal(2); } + if (msg.parts.index === 1) { msg.payload.length.should.equal(1); done(); } + }); + var b = new Buffer.from("123"); + sn1.receive({ payload: b }); + }); + }); + + it('should set msg.parts.ch when node.spltType is str', function (done) { + var flow = [{ id: "sn1", type: "split", splt: "2", spltType: "str", stream: false, wires: [["sn2"]] }, + { id: "sn2", type: "helper" }]; + helper.load(splitNode, flow, function () { + var sn1 = helper.getNode("sn1"); + var sn2 = helper.getNode("sn2"); + sn2.on("input", function (msg) { + msg.should.have.property("parts"); + msg.parts.should.have.property("count", 2); + msg.parts.should.have.property("index"); + if (msg.parts.index === 0) { msg.payload.length.should.equal(2); } + if (msg.parts.index === 1) { msg.payload.length.should.equal(1); done(); } + }); + var b = new Buffer.from("123"); + sn1.receive({ payload: b }); + }); + }); + }); describe('JOIN node', function() { @@ -942,7 +1064,7 @@ describe('JOIN node', function() { evt.should.have.property('type', "join"); evt.should.have.property('msg', "join.too-many"); done(); - }, 150); + }, TimeoutForErrorCase); n1.receive({payload:3, parts:{index:2, count:4, id:222}}); n1.receive({payload:2, parts:{index:1, count:4, id:222}}); n1.receive({payload:4, parts:{index:3, count:4, id:222}}); @@ -950,4 +1072,109 @@ describe('JOIN node', function() { }); }); + it('should handle invalid JSON expression"', function (done) { + var flow = [{ + id: "n1", type: "join", mode: "reduce", + reduceRight: false, + reduceExp: "invalid expr", + reduceInit: "0", + reduceInitType: "num", + reduceFixup: undefined, + wires: [["n2"]] + }, + { id: "n2", type: "helper" }]; + helper.load(joinNode, flow, function () { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + setTimeout(function () { + done(); + }, TimeoutForErrorCase); + n2.on("input", function (msg) { + done(new Error("This path does not go through.")); + }); + n1.receive({ payload: "A", parts: { id: 1, type: "string", ch: ",", index: 0, count: 1 } }); + }); + }); + + it('should concat payload when group.type is array', function (done) { + var flow = [{ id: "n1", type: "join", wires: [["n2"]], build: "array", mode: "auto" }, + { id: "n2", type: "helper" }]; + helper.load(joinNode, flow, function () { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n2.on("input", function (msg) { + try { + msg.should.have.property("payload"); + msg.payload.should.be.an.Array(); + msg.payload[0].should.equal("ab"); + msg.payload[1].should.equal("cd"); + msg.payload[2].should.equal("ef"); + done(); + } + catch (e) { done(e); } + }); + n1.receive({ payload: "ab", parts: { id: 1, type: "array", ch: ",", index: 0, count: 3, len:2}}); + n1.receive({ payload: "cd", parts: { id: 1, type: "array", ch: ",", index: 1, count: 3, len:2}}); + n1.receive({ payload: "ef", parts: { id: 1, type: "array", ch: ",", index: 2, count: 3, len:2}}); + }); + }); + + it('should concat payload when group.type is buffer and group.joinChar is undefined', function (done) { + var flow = [{ id: "n1", type: "join", wires: [["n2"]], joiner: ",", build: "buffer", mode: "auto" }, + { id: "n2", type: "helper" }]; + helper.load(joinNode, flow, function () { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n2.on("input", function (msg) { + try { + msg.should.have.property("payload"); + Buffer.isBuffer(msg.payload).should.be.true(); + msg.payload.toString().should.equal("ABC"); + done(); + } + catch (e) { done(e); } + }); + n1.receive({ payload: Buffer.from("A"), parts: { id: 1, type: "buffer", index: 0, count: 3 } }); + n1.receive({ payload: Buffer.from("B"), parts: { id: 1, type: "buffer", index: 1, count: 3 } }); + n1.receive({ payload: Buffer.from("C"), parts: { id: 1, type: "buffer", index: 2, count: 3 } }); + }); + }); + + it('should concat payload when group.type is string and group.joinChar is not string', function (done) { + var flow = [{ id: "n1", type: "join", wires: [["n2"]], joiner: ",", build: "buffer", mode: "auto" }, + { id: "n2", type: "helper" }]; + helper.load(joinNode, flow, function () { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n2.on("input", function (msg) { + try { + msg.should.have.property("payload"); + msg.payload.toString().should.equal("A0B0C"); + done(); + } + catch (e) { done(e); } + }); + n1.receive({ payload: Buffer.from("A"), parts: { id: 1, type: "string", ch: Buffer.from("0"), index: 0, count: 3 } }); + n1.receive({ payload: Buffer.from("B"), parts: { id: 1, type: "string", ch: Buffer.from("0"), index: 1, count: 3 } }); + n1.receive({ payload: Buffer.from("C"), parts: { id: 1, type: "string", ch: Buffer.from("0"), index: 2, count: 3 } }); + }); + }); + + it('should handle msg.parts property when mode is auto and parts or id are missing', function (done) { + var flow = [{ id: "n1", type: "join", wires: [["n2"]], joiner: "[44]", joinerType: "bin", build: "string", mode: "auto" }, + { id: "n2", type: "helper" }]; + helper.load(joinNode, flow, function () { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n2.on("input", function (msg) { + done(new Error("This path does not go through.")); + }); + n1.receive({ payload: "A", parts: { type: "string", ch: ",", index: 0, count: 2 } }); + n1.receive({ payload: "B", parts: { type: "string", ch: ",", index: 1, count: 2 } }); + setTimeout(function () { + done(); + }, TimeoutForErrorCase); + }); + }); + }); diff --git a/test/nodes/core/logic/18-sort_spec.js b/test/nodes/core/logic/18-sort_spec.js index a585d7dd5..e92974523 100644 --- a/test/nodes/core/logic/18-sort_spec.js +++ b/test/nodes/core/logic/18-sort_spec.js @@ -213,8 +213,6 @@ describe('SORT node', function() { check_sort1C(flow, "$substring(payload,1)", data_in, data_out, done); }); })(); - - return; (function() { var flow = [{id:"n1", type:"sort", order:"descending", as_num:false, wires:[["n2"]]}, @@ -250,8 +248,6 @@ describe('SORT node', function() { n1.receive(msg1); }); }); - - return; it('should handle too many pending messages', function(done) { var flow = [{id:"n1", type:"sort", order:"ascending", as_num:false, target:"payload", targetType:"seq", seqKey:"payload", seqKeyType:"msg", wires:[["n2"]]},