From 937d79d28fb180be0314d7c3f2745ea90eafedf2 Mon Sep 17 00:00:00 2001 From: Hiroki Uchikawa <31908137+HirokiUchikawa@users.noreply.github.com> Date: Fri, 13 Oct 2017 04:47:13 +0900 Subject: [PATCH] Add test cases for debug node (#1438) --- test/nodes/core/core/58-debug_spec.js | 218 ++++++++++++++++++++++++++ 1 file changed, 218 insertions(+) diff --git a/test/nodes/core/core/58-debug_spec.js b/test/nodes/core/core/58-debug_spec.js index 5c211f79b..901016da9 100644 --- a/test/nodes/core/core/58-debug_spec.js +++ b/test/nodes/core/core/58-debug_spec.js @@ -100,6 +100,34 @@ describe('debug node', function() { }); }); + it('should publish complete message to console', function(done) { + var flow = [{id:"n1", type:"debug", complete: "true", console: "true" }]; + helper.load(debugNode, flow, function() { + var n1 = helper.getNode("n1"); + websocket_test(function() { + n1.emit("input", {payload:"test"}); + }, function(msg) { + JSON.parse(msg).should.eql({ + topic:"debug", + data:{id:"n1",msg:'{\n "payload": "test"\n}',format:"Object"} + }); + }, function() { + try { + helper.log().called.should.be.true(); + var logEvents = helper.log().args.filter(function(evt) { + return evt[0].type == "debug"; + }); + logEvents.should.have.length(1); + var tstmp = logEvents[0][0].timestamp; + logEvents[0][0].should.eql({level:helper.log().INFO, id:"n1",type:"debug",msg:'\n{ payload: \'test\' }',timestamp:tstmp}); + done(); + } catch(err) { + done(err); + } + }); + }); + }); + it('should publish other property', function(done) { var flow = [{id:"n1", type:"debug", complete: "foo" }]; helper.load(debugNode, flow, function() { @@ -156,6 +184,20 @@ describe('debug node', function() { }); }); + it('should publish a number', function(done) { + var flow = [{id:"n1", type:"debug", console:"true" }]; + helper.load(debugNode, flow, function() { + var n1 = helper.getNode("n1"); + websocket_test(function() { + n1.emit("input", {payload: 7}); + }, function(msg) { + JSON.parse(msg).should.eql({ + topic:"debug",data:{id:"n1",msg:"7",property:"payload",format:"number"} + }); + }, done); + }); + }); + it('should publish with no payload', function(done) { var flow = [{id:"n1", type:"debug" }]; helper.load(debugNode, flow, function() { @@ -170,6 +212,20 @@ describe('debug node', function() { }); }); + it('should publish a null', function(done) { + var flow = [{id:"n1", type:"debug" }]; + helper.load(debugNode, flow, function() { + var n1 = helper.getNode("n1"); + websocket_test(function() { + n1.emit("input", {payload: null}); + }, function(msg) { + JSON.parse(msg).should.eql({ + topic:"debug",data:{id:"n1",msg:'(undefined)',property:"payload",format:"null"} + }); + }, done); + }); + }); + it('should publish an object', function(done) { var flow = [{id:"n1", type:"debug" }]; helper.load(debugNode, flow, function() { @@ -222,6 +278,60 @@ describe('debug node', function() { }); }); + it('should publish an object to console', function(done) { + var flow = [{id:"n1", type:"debug", console: "true"}]; + helper.load(debugNode, flow, function() { + var n1 = helper.getNode("n1"); + websocket_test(function() { + n1.emit("input", {payload: {type:'foo'}}); + }, function(msg) { + JSON.parse(msg).should.eql({ + topic:"debug",data:{id:"n1",msg:'{\n "type": "foo"\n}',property:"payload",format:"Object"} + }); + }, function() { + try { + helper.log().called.should.be.true(); + var logEvents = helper.log().args.filter(function(evt) { + return evt[0].type == "debug"; + }); + logEvents.should.have.length(1); + var tstmp = logEvents[0][0].timestamp; + logEvents[0][0].should.eql({level:helper.log().INFO,id:"n1",type:"debug",msg:'\n{ type: \'foo\' }',timestamp:tstmp}); + done(); + } catch(err) { + done(err); + } + }); + }); + }); + + it('should publish a string after a newline to console if the string contains \\n', function(done) { + var flow = [{id:"n1", type:"debug", console: "true"}]; + helper.load(debugNode, flow, function() { + var n1 = helper.getNode("n1"); + websocket_test(function() { + n1.emit("input", {payload: "test\ntest"}); + }, function(msg) { + JSON.parse(msg).should.eql({ + topic:"debug",data:{id:"n1",msg:"test\ntest",property:"payload",format:"string[9]"} + }); + }, function() { + try { + helper.log().called.should.be.true(); + var logEvents = helper.log().args.filter(function(evt) { + return evt[0].type == "debug"; + }); + logEvents.should.have.length(1); + var tstmp = logEvents[0][0].timestamp; + logEvents[0][0].should.eql({level:helper.log().INFO,id:"n1",type:"debug",msg:"\ntest\ntest",timestamp:tstmp}); + done(); + } catch(err) { + done(err); + } + }); + }); + }); + it('should truncate a long message', function(done) { var flow = [{id:"n1", type:"debug" }]; helper.load(debugNode, flow, function() { @@ -243,6 +353,102 @@ describe('debug node', function() { }); }); + it('should truncate a long string in the object', function(done) { + var flow = [{id:"n1", type:"debug"}]; + helper.load(debugNode, flow, function() { + var n1 = helper.getNode("n1"); + websocket_test(function() { + n1.emit("input", {payload: {foo: Array(1002).join("X")}}); + }, function(msg) { + var a = JSON.parse(msg); + a.should.eql({ + topic:"debug", + data:{ + id:"n1", + msg:'{\n "foo": "'+Array(1001).join("X")+'..."\n}', + property:"payload", + format:"Object" + } + }); + }, done); + }); + }); + + it('should truncate a large array', function(done) { + var flow = [{id:"n1", type:"debug" }]; + helper.load(debugNode, flow, function() { + var n1 = helper.getNode("n1"); + websocket_test(function() { + n1.emit("input", {payload: Array(1001).fill("X")}); + }, function(msg) { + var a = JSON.parse(msg); + a.should.eql({ + topic:"debug", + data:{ + id:"n1", + msg:JSON.stringify({ + __encoded__: true, + type: "array", + data: Array(1000).fill("X"), + length: 1001 + },null," "), + property:"payload", + format:"array[1001]" + } + }); + }, done); + }); + }); + + it('should truncate a large array in the object', function(done) { + var flow = [{id:"n1", type:"debug"}]; + helper.load(debugNode, flow, function() { + var n1 = helper.getNode("n1"); + websocket_test(function() { + n1.emit("input", {payload: {foo: Array(1001).fill("X")}}); + }, function(msg) { + var a = JSON.parse(msg); + a.should.eql({ + topic:"debug", + data:{ + id:"n1", + msg:JSON.stringify({ + foo:{ + __encoded__: true, + type: "array", + data: Array(1000).fill("X"), + length: 1001 + } + },null," "), + property:"payload", + format:"Object" + } + }); + }, done); + }); + }); + + it('should truncate a large buffer', function(done) { + var flow = [{id:"n1", type:"debug" }]; + helper.load(debugNode, flow, function() { + var n1 = helper.getNode("n1"); + websocket_test(function() { + n1.emit("input", {payload: Buffer(501).fill("\"")}); + }, function(msg) { + var a = JSON.parse(msg); + a.should.eql({ + topic:"debug", + data:{ + id:"n1", + msg: Array(1001).join("2"), + property:"payload", + format:"buffer[501]" + } + }); + }, done); + }); + }); + it('should convert Buffer to hex', function(done) { var flow = [{id:"n1", type:"debug" }]; helper.load(debugNode, flow, function() { @@ -324,6 +530,18 @@ describe('debug node', function() { }); }); + describe('get', function() { + it('should return the view.html', function(done) { + var flow = [{id:"n1", type:"debug"}]; + helper.load(debugNode, flow, function() { + helper.request() + .get('/debug/view/view.html') + .expect(200) + .end(done); + }); + }); + }); + }); function websocket_test(open_callback, message_callback, done_callback) {