PageRenderTime 1301ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/test/nodes/core/core/58-debug_spec.js

https://gitlab.com/farm-assistant/nodered
JavaScript | 338 lines | 297 code | 26 blank | 15 comment | 3 complexity | 8165546d75eb60d5ba799bf376896e19 MD5 | raw file
  1. /**
  2. * Copyright JS Foundation and other contributors, http://js.foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. **/
  16. var should = require("should");
  17. var debugNode = require("../../../../nodes/core/core/58-debug.js");
  18. var helper = require("../../helper.js");
  19. var WebSocket = require('ws');
  20. describe('debug node', function() {
  21. before(function(done) {
  22. helper.startServer(done);
  23. });
  24. afterEach(function() {
  25. helper.unload();
  26. });
  27. it('should be loaded', function(done) {
  28. var flow = [{id:"n1", type:"debug", name: "Debug", complete:"false" }];
  29. helper.load(debugNode, flow, function() {
  30. var n1 = helper.getNode("n1");
  31. n1.should.have.property('name', 'Debug');
  32. n1.should.have.property('complete', "payload");
  33. done();
  34. });
  35. });
  36. it('should publish on input', function(done) {
  37. var flow = [{id:"n1", type:"debug", name: "Debug" }];
  38. helper.load(debugNode, flow, function() {
  39. var n1 = helper.getNode("n1");
  40. websocket_test(function() {
  41. n1.emit("input", {payload:"test"});
  42. }, function(msg) {
  43. JSON.parse(msg).should.eql({
  44. topic:"debug",data:{id:"n1",name:"Debug",msg:"test",
  45. format:"string[4]",property:"payload"}
  46. });
  47. }, done);
  48. });
  49. });
  50. it('should publish to console', function(done) {
  51. var flow = [{id:"n1", type:"debug", console: "true"}];
  52. helper.load(debugNode, flow, function() {
  53. var n1 = helper.getNode("n1");
  54. var count = 0;
  55. websocket_test(function() {
  56. n1.emit("input", {payload:"test"});
  57. }, function(msg) {
  58. JSON.parse(msg).should.eql({
  59. topic:"debug",data:{id:"n1",msg:"test",property:"payload",format:"string[4]"}
  60. });
  61. count++;
  62. }, function() {
  63. try {
  64. helper.log().called.should.be.true();
  65. var logEvents = helper.log().args.filter(function(evt) {
  66. return evt[0].type == "debug";
  67. });
  68. logEvents.should.have.length(1);
  69. var tstmp = logEvents[0][0].timestamp;
  70. logEvents[0][0].should.eql({level:helper.log().INFO, id:'n1',type:'debug',msg:'test', timestamp:tstmp});
  71. done();
  72. } catch(err) {
  73. done(err);
  74. }
  75. });
  76. });
  77. });
  78. it('should publish complete message', function(done) {
  79. var flow = [{id:"n1", type:"debug", complete: "true" }];
  80. helper.load(debugNode, flow, function() {
  81. var n1 = helper.getNode("n1");
  82. websocket_test(function() {
  83. n1.emit("input", {payload:"test"});
  84. }, function(msg) {
  85. JSON.parse(msg).should.eql({
  86. topic:"debug",
  87. data:{id:"n1",msg:'{\n "payload": "test"\n}',format:"Object"}
  88. });
  89. }, done);
  90. });
  91. });
  92. it('should publish other property', function(done) {
  93. var flow = [{id:"n1", type:"debug", complete: "foo" }];
  94. helper.load(debugNode, flow, function() {
  95. var n1 = helper.getNode("n1");
  96. websocket_test(function() {
  97. n1.emit("input", {payload:"test", foo:"bar"});
  98. }, function(msg) {
  99. JSON.parse(msg).should.eql({
  100. topic:"debug",data:{id:"n1",msg:"bar",property:"foo",format:"string[3]"}
  101. });
  102. }, done);
  103. });
  104. });
  105. it('should publish multi-level properties', function(done) {
  106. var flow = [{id:"n1", type:"debug", complete: "foo.bar" }];
  107. helper.load(debugNode, flow, function() {
  108. var n1 = helper.getNode("n1");
  109. websocket_test(function() {
  110. n1.emit("input", {payload:"test", foo: {bar: "bar"}});
  111. }, function(msg) {
  112. JSON.parse(msg).should.eql({
  113. topic:"debug",data:{id:"n1",msg:"bar",property:"foo.bar",format:"string[3]"}
  114. });
  115. }, done);
  116. });
  117. });
  118. it('should publish an Error', function(done) {
  119. var flow = [{id:"n1", type:"debug" }];
  120. helper.load(debugNode, flow, function() {
  121. var n1 = helper.getNode("n1");
  122. websocket_test(function() {
  123. n1.emit("input", {payload: new Error("oops")});
  124. }, function(msg) {
  125. JSON.parse(msg).should.eql({
  126. topic:"debug",data:{id:"n1",msg:"Error: oops",property:"payload",format:"error"}
  127. });
  128. }, done);
  129. });
  130. });
  131. it('should publish a boolean', function(done) {
  132. var flow = [{id:"n1", type:"debug" }];
  133. helper.load(debugNode, flow, function() {
  134. var n1 = helper.getNode("n1");
  135. websocket_test(function() {
  136. n1.emit("input", {payload: true});
  137. }, function(msg) {
  138. JSON.parse(msg).should.eql({
  139. topic:"debug",data:{id:"n1",msg: 'true',property:"payload",format:"boolean"}
  140. });
  141. }, done);
  142. });
  143. });
  144. it('should publish with no payload', function(done) {
  145. var flow = [{id:"n1", type:"debug" }];
  146. helper.load(debugNode, flow, function() {
  147. var n1 = helper.getNode("n1");
  148. websocket_test(function() {
  149. n1.emit("input", {});
  150. }, function(msg) {
  151. JSON.parse(msg).should.eql({
  152. topic:"debug",data:{id:"n1",msg: '(undefined)',property:"payload",format:"undefined"}
  153. });
  154. }, done);
  155. });
  156. });
  157. it('should publish an object', function(done) {
  158. var flow = [{id:"n1", type:"debug" }];
  159. helper.load(debugNode, flow, function() {
  160. var n1 = helper.getNode("n1");
  161. websocket_test(function() {
  162. n1.emit("input", {payload: {type:'foo'}});
  163. }, function(msg) {
  164. JSON.parse(msg).should.eql({
  165. topic:"debug",
  166. data:{id:"n1",msg:'{\n "type": "foo"\n}',property:"payload",format:"Object"}
  167. });
  168. }, done);
  169. });
  170. });
  171. it('should publish an array', function(done) {
  172. var flow = [{id:"n1", type:"debug" }];
  173. helper.load(debugNode, flow, function() {
  174. var n1 = helper.getNode("n1");
  175. websocket_test(function() {
  176. n1.emit("input", {payload: [0,1,2,3]});
  177. }, function(msg) {
  178. JSON.parse(msg).should.eql({
  179. topic:"debug",
  180. data:{id:"n1",msg: '[\n 0,\n 1,\n 2,\n 3\n]',format:"array[4]",
  181. property:"payload"}
  182. });
  183. }, done);
  184. });
  185. });
  186. it('should publish an object with circular references', function(done) {
  187. var flow = [{id:"n1", type:"debug" }];
  188. helper.load(debugNode, flow, function() {
  189. var n1 = helper.getNode("n1");
  190. websocket_test(function() {
  191. var o = { name: 'bar' };
  192. o.o = o;
  193. n1.emit("input", {payload: o});
  194. }, function(msg) {
  195. JSON.parse(msg).should.eql({
  196. topic:"debug",
  197. data:{
  198. id:"n1",
  199. msg:'{\n "name": "bar",\n "o": "[Circular ~]"\n}',
  200. property:"payload",format:"Object"
  201. }
  202. });
  203. }, done);
  204. });
  205. });
  206. it('should truncate a long message', function(done) {
  207. var flow = [{id:"n1", type:"debug" }];
  208. helper.load(debugNode, flow, function() {
  209. var n1 = helper.getNode("n1");
  210. websocket_test(function() {
  211. n1.emit("input", {payload: Array(1002).join("X")});
  212. }, function(msg) {
  213. var a = JSON.parse(msg);
  214. a.should.eql({
  215. topic:"debug",
  216. data:{
  217. id:"n1",
  218. msg: Array(1001).join("X")+'...',
  219. property:"payload",
  220. format:"string[1001]"
  221. }
  222. });
  223. }, done);
  224. });
  225. });
  226. it('should convert Buffer to hex', function(done) {
  227. var flow = [{id:"n1", type:"debug" }];
  228. helper.load(debugNode, flow, function() {
  229. var n1 = helper.getNode("n1");
  230. websocket_test(function() {
  231. n1.emit("input", {payload: new Buffer('HELLO', 'utf8')});
  232. }, function(msg) {
  233. JSON.parse(msg).should.eql({
  234. topic:"debug",
  235. data:{
  236. id:"n1",
  237. msg: '48454c4c4f',
  238. property:"payload",
  239. format: "buffer[5]"
  240. }
  241. });
  242. }, done);
  243. });
  244. });
  245. it('should publish when active', function(done) {
  246. var flow = [{id:"n1", type:"debug", active: false }];
  247. helper.load(debugNode, flow, function() {
  248. var n1 = helper.getNode("n1");
  249. websocket_test(function() {
  250. n1.emit("input", {payload:"message 1"});
  251. helper.request()
  252. .post('/debug/n1/enable')
  253. .expect(200).end(function(err) {
  254. if (err) { return done(err); }
  255. n1.emit("input", {payload:"message 2"});
  256. });
  257. }, function(msg) {
  258. JSON.parse(msg).should.eql({
  259. topic:"debug",data:{id:"n1",msg:"message 2",property:"payload",format:"string[9]"}
  260. });
  261. }, done);
  262. });
  263. });
  264. it('should not publish when inactive', function(done) {
  265. var flow = [{id:"n1", type:"debug", active: true }];
  266. helper.load(debugNode, flow, function() {
  267. var n1 = helper.getNode("n1");
  268. websocket_test(function(close) {
  269. helper.request()
  270. .post('/debug/n1/disable')
  271. .expect(201).end(function(err) {
  272. if (err) {
  273. close();
  274. return done(err);
  275. }
  276. n1.emit("input", {payload:"message"});
  277. setTimeout(function() {
  278. close();
  279. done();
  280. }, 200);
  281. });
  282. }, function(msg) {
  283. should.fail(null,null,"unexpected message");
  284. }, function() {});
  285. });
  286. });
  287. describe('post', function() {
  288. it('should return 404 on invalid state', function(done) {
  289. var flow = [{id:"n1", type:"debug", active: true }];
  290. helper.load(debugNode, flow, function() {
  291. helper.request()
  292. .post('/debug/n1/foobar')
  293. .expect(404).end(done);
  294. });
  295. });
  296. it('should return 404 on invalid node', function(done) {
  297. helper.request()
  298. .post('/debug/n99/enable')
  299. .expect(404).end(done);
  300. });
  301. });
  302. });
  303. function websocket_test(open_callback, message_callback, done_callback) {
  304. var ws = new WebSocket(helper.url() + "/comms");
  305. var close_callback = function() { ws.close(); };
  306. ws.on('open', function() { open_callback(close_callback); });
  307. ws.on('message', function(msg) {
  308. message_callback(msg, close_callback);
  309. ws.close();
  310. done_callback();
  311. });
  312. }