mirror of
				https://github.com/node-red/node-red.git
				synced 2025-03-01 10:36:34 +00:00 
			
		
		
		
	boost api index, nodes index and nodes Node test coverage
This commit is contained in:
		@@ -29,7 +29,7 @@ function Node(n) {
 | 
			
		||||
    this.type = n.type;
 | 
			
		||||
    this.z = n.z;
 | 
			
		||||
    this._closeCallbacks = [];
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    if (n.name) {
 | 
			
		||||
        this.name = n.name;
 | 
			
		||||
    }
 | 
			
		||||
@@ -41,7 +41,7 @@ util.inherits(Node, EventEmitter);
 | 
			
		||||
Node.prototype.updateWires = function(wires) {
 | 
			
		||||
    this.wires = wires || [];
 | 
			
		||||
    delete this._wire;
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    var wc = 0;
 | 
			
		||||
    this.wires.forEach(function(w) {
 | 
			
		||||
        wc+=w.length;
 | 
			
		||||
@@ -103,10 +103,10 @@ function constructUniqueIdentifier() {
 | 
			
		||||
Node.prototype.send = function(msg) {
 | 
			
		||||
    var msgSent = false;
 | 
			
		||||
    var node;
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    if (msg === null || typeof msg === "undefined") {
 | 
			
		||||
        return;
 | 
			
		||||
    } else if (!util.isArray(msg)) {       
 | 
			
		||||
    } else if (!util.isArray(msg)) {
 | 
			
		||||
        if (this._wire) {
 | 
			
		||||
            // A single message and a single wire on output 0
 | 
			
		||||
            // TODO: pre-load flows.get calls - cannot do in constructor
 | 
			
		||||
@@ -116,6 +116,7 @@ Node.prototype.send = function(msg) {
 | 
			
		||||
            }
 | 
			
		||||
            this.metric("send",msg);
 | 
			
		||||
            node = flows.get(this._wire);
 | 
			
		||||
            /* istanbul ignore else */
 | 
			
		||||
            if (node) {
 | 
			
		||||
                node.receive(msg);
 | 
			
		||||
            }
 | 
			
		||||
@@ -124,18 +125,19 @@ Node.prototype.send = function(msg) {
 | 
			
		||||
            msg = [msg];
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    var numOutputs = this.wires.length;
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    // Build a list of send events so that all cloning is done before
 | 
			
		||||
    // any calls to node.receive
 | 
			
		||||
    var sendEvents = [];
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    var sentMessageId = null;
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    // for each output of node eg. [msgs to output 0, msgs to output 1, ...]
 | 
			
		||||
    for (var i = 0; i < numOutputs; i++) {
 | 
			
		||||
        var wires = this.wires[i]; // wires leaving output i
 | 
			
		||||
        /* istanbul ignore else */
 | 
			
		||||
        if (i < msg.length) {
 | 
			
		||||
            var msgs = msg[i]; // msgs going to output i
 | 
			
		||||
            if (msgs !== null && typeof msgs !== "undefined") {
 | 
			
		||||
@@ -150,6 +152,7 @@ Node.prototype.send = function(msg) {
 | 
			
		||||
                        // for each msg to send eg. [[m1, m2, ...], ...]
 | 
			
		||||
                        for (k = 0; k < msgs.length; k++) {
 | 
			
		||||
                            var m = msgs[k];
 | 
			
		||||
                            /* istanbul ignore else */
 | 
			
		||||
                            if (!sentMessageId) {
 | 
			
		||||
                                sentMessageId = m._msgid;
 | 
			
		||||
                            }
 | 
			
		||||
@@ -166,13 +169,15 @@ Node.prototype.send = function(msg) {
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    /* istanbul ignore else */
 | 
			
		||||
    if (!sentMessageId) {
 | 
			
		||||
        sentMessageId = constructUniqueIdentifier();
 | 
			
		||||
    }
 | 
			
		||||
    this.metric("send",{_msgid:sentMessageId});
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    for (i=0;i<sendEvents.length;i++) {
 | 
			
		||||
        var ev = sendEvents[i];
 | 
			
		||||
        /* istanbul ignore else */
 | 
			
		||||
        if (!ev.m._msgid) {
 | 
			
		||||
            ev.m._msgid = sentMessageId;
 | 
			
		||||
        }
 | 
			
		||||
@@ -180,14 +185,14 @@ Node.prototype.send = function(msg) {
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
Node.prototype.receive = function(msg) {     
 | 
			
		||||
Node.prototype.receive = function(msg) {
 | 
			
		||||
    if (!msg) {
 | 
			
		||||
        msg = {};
 | 
			
		||||
    }
 | 
			
		||||
    if (!msg._msgid) {
 | 
			
		||||
        msg._msgid = constructUniqueIdentifier();
 | 
			
		||||
    }
 | 
			
		||||
    this.metric("receive",msg); 
 | 
			
		||||
    this.metric("receive",msg);
 | 
			
		||||
    this.emit("input", msg);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
@@ -215,6 +220,7 @@ Node.prototype.warn = function(msg) {
 | 
			
		||||
Node.prototype.error = function(logMessage,msg) {
 | 
			
		||||
    logMessage = logMessage || "";
 | 
			
		||||
    log_helper(this, Log.ERROR, logMessage);
 | 
			
		||||
    /* istanbul ignore else */
 | 
			
		||||
    if (msg) {
 | 
			
		||||
        flows.handleError(this,logMessage,msg);
 | 
			
		||||
    }
 | 
			
		||||
@@ -230,7 +236,7 @@ Node.prototype.metric = function(eventname, msg, metricValue) {
 | 
			
		||||
    var metrics = {};
 | 
			
		||||
    metrics.level = Log.METRIC;
 | 
			
		||||
    metrics.nodeid = this.id;
 | 
			
		||||
    metrics.event = "node."+this.type+"."+eventname;    
 | 
			
		||||
    metrics.event = "node."+this.type+"."+eventname;
 | 
			
		||||
    metrics.msgid = msg._msgid;
 | 
			
		||||
    metrics.value = metricValue;
 | 
			
		||||
    Log.log(metrics);
 | 
			
		||||
 
 | 
			
		||||
@@ -19,24 +19,22 @@ var request = require("supertest");
 | 
			
		||||
var express = require("express");
 | 
			
		||||
var fs = require("fs");
 | 
			
		||||
var path = require("path");
 | 
			
		||||
 | 
			
		||||
var settings = require("../../../red/settings");
 | 
			
		||||
var api = require("../../../red/api");
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
describe("api index", function() {
 | 
			
		||||
    var app;
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    describe("disables editor", function() {
 | 
			
		||||
        before(function() {
 | 
			
		||||
            settings.init({disableEditor:true});
 | 
			
		||||
            settings.init({disableEditor:true,adminAuth:{type: "credentials",users:[],default:{permissions:"read"}}});
 | 
			
		||||
            app = express();
 | 
			
		||||
            api.init(app);
 | 
			
		||||
        });
 | 
			
		||||
        after(function() {
 | 
			
		||||
            settings.reset();
 | 
			
		||||
        });
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        it('does not serve the editor', function(done) {
 | 
			
		||||
            request(app)
 | 
			
		||||
                .get("/")
 | 
			
		||||
@@ -53,7 +51,7 @@ describe("api index", function() {
 | 
			
		||||
                .expect(200,done)
 | 
			
		||||
        });
 | 
			
		||||
    });
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    describe("enables editor", function() {
 | 
			
		||||
        before(function() {
 | 
			
		||||
            settings.init({disableEditor:false});
 | 
			
		||||
@@ -63,7 +61,7 @@ describe("api index", function() {
 | 
			
		||||
        after(function() {
 | 
			
		||||
            settings.reset();
 | 
			
		||||
        });
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        it('serves the editor', function(done) {
 | 
			
		||||
            request(app)
 | 
			
		||||
                .get("/")
 | 
			
		||||
@@ -88,5 +86,10 @@ describe("api index", function() {
 | 
			
		||||
                .get("/settings")
 | 
			
		||||
                .expect(200,done)
 | 
			
		||||
        });
 | 
			
		||||
        it('handles page not there', function(done) {
 | 
			
		||||
            request(app)
 | 
			
		||||
                .get("/foo")
 | 
			
		||||
                .expect(404,done)
 | 
			
		||||
        });
 | 
			
		||||
    });
 | 
			
		||||
});
 | 
			
		||||
});
 | 
			
		||||
 
 | 
			
		||||
@@ -23,7 +23,7 @@ describe("red/log", function() {
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    var log = require("../../red/log");
 | 
			
		||||
    var sett = {logging: { console: { level: 'metric', metrics: true } } }
 | 
			
		||||
    var sett = {logging: { console: { level: 'metric', metrics: true } } };
 | 
			
		||||
    log.init(sett);
 | 
			
		||||
 | 
			
		||||
    beforeEach(function () {
 | 
			
		||||
 
 | 
			
		||||
@@ -417,14 +417,13 @@ describe('Node', function() {
 | 
			
		||||
            });
 | 
			
		||||
            
 | 
			
		||||
            var message = {a:1};
 | 
			
		||||
            
 | 
			
		||||
            n.error("an error message",message);
 | 
			
		||||
            should.deepEqual({level:Log.ERROR, id:n.id,
 | 
			
		||||
                             type:n.type, msg:"an error message"}, loginfo);
 | 
			
		||||
            
 | 
			
		||||
 | 
			
		||||
            n.error(null,message);
 | 
			
		||||
            should.deepEqual({level:Log.ERROR, id:n.id, type:n.type, msg:""}, loginfo);
 | 
			
		||||
 | 
			
		||||
            flows.handleError.called.should.be.true;
 | 
			
		||||
            flows.handleError.args[0][0].should.eql(n);
 | 
			
		||||
            flows.handleError.args[0][1].should.eql("an error message");
 | 
			
		||||
            flows.handleError.args[0][1].should.eql("");
 | 
			
		||||
            flows.handleError.args[0][2].should.eql(message);
 | 
			
		||||
            
 | 
			
		||||
            Log.log.restore();
 | 
			
		||||
@@ -448,8 +447,22 @@ describe('Node', function() {
 | 
			
		||||
            done();
 | 
			
		||||
        });
 | 
			
		||||
    });
 | 
			
		||||
    
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    describe('#metric', function() {
 | 
			
		||||
        it('returns metric value if eventname undefined', function(done) {
 | 
			
		||||
            var n = new RedNode({id:'123',type:'abc'});
 | 
			
		||||
            var loginfo = {};
 | 
			
		||||
            sinon.stub(Log, 'log', function(msg) {
 | 
			
		||||
                loginfo = msg;
 | 
			
		||||
            });
 | 
			
		||||
            var msg = {payload:"foo", _msgid:"987654321"};
 | 
			
		||||
            var m = n.metric(undefined,msg,"15mb");
 | 
			
		||||
            m.should.equal(true);
 | 
			
		||||
            Log.log.restore();
 | 
			
		||||
            done();
 | 
			
		||||
        });
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    describe('#status', function() {
 | 
			
		||||
        after(function() {
 | 
			
		||||
            comms.publish.restore();
 | 
			
		||||
 
 | 
			
		||||
@@ -58,7 +58,6 @@ describe("red/nodes/index", function() {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
   it('nodes are initialised with credentials',function(done) {
 | 
			
		||||
 | 
			
		||||
        index.init(settings, storage);
 | 
			
		||||
        index.registerType('test', TestNode);
 | 
			
		||||
        index.loadFlows().then(function() {
 | 
			
		||||
@@ -69,7 +68,6 @@ describe("red/nodes/index", function() {
 | 
			
		||||
        }).otherwise(function(err) {
 | 
			
		||||
            done(err);
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
   it('flows should be initialised',function(done) {
 | 
			
		||||
@@ -128,7 +126,7 @@ describe("red/nodes/index", function() {
 | 
			
		||||
                   foo: {type:"test"}
 | 
			
		||||
               }
 | 
			
		||||
           });
 | 
			
		||||
           var testnode = new TestNode({id:'tab1',type:'test',name:'barney'});
 | 
			
		||||
           var testnode = new TestNode({id:'tab1',type:'test',name:'barney', '_alias':'tab1'});
 | 
			
		||||
           credentials.getDefinition("test").should.have.property('foo');
 | 
			
		||||
           done();
 | 
			
		||||
       });
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user