1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Add tests for pluggable library

This commit is contained in:
Nick O'Leary 2021-02-19 15:24:56 +00:00
parent 6f9e06e78d
commit b41c7962c2
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
3 changed files with 140 additions and 41 deletions

View File

@ -28,9 +28,34 @@ let runtimeLibraries = [];
// Libraries defined by the user in the editor. // Libraries defined by the user in the editor.
let userLibraries = []; let userLibraries = [];
function init(runtime) { let runtime;
events.on("registry:plugin-added", function(id) { function init(_runtime) {
runtime = _runtime;
events.removeListener("registry:plugin-added",onPluginAdded);
events.on("registry:plugin-added",onPluginAdded);
knownTypes.flows = 'node-red';
libraries["local"] = require("./local");
libraries["local"].init(runtime);
libraryConfigs["local"] = libraries["local"]
libraries["examples"] = require("./examples");
libraries["examples"].init(runtime);
libraryConfigs["examples"] = libraries["examples"]
try {
runtimeLibraries = runtime.settings.editorTheme.library.sources;
} catch(err) {
runtimeLibraries = [];
}
// userLibraries = runtime.settings.get("library")
}
function onPluginAdded(id) {
const plugin = runtime.plugins.getPlugin(id); const plugin = runtime.plugins.getPlugin(id);
if (plugin.type === "node-red-library-source") { if (plugin.type === "node-red-library-source") {
libraryPlugins[plugin.id] = plugin; libraryPlugins[plugin.id] = plugin;
@ -62,27 +87,9 @@ function init(runtime) {
} }
})
knownTypes.flows = 'node-red';
libraries["local"] = require("./local");
libraries["local"].init(runtime);
libraryConfigs["local"] = libraries["local"]
libraries["examples"] = require("./examples");
libraries["examples"].init(runtime);
libraryConfigs["examples"] = libraries["examples"]
try {
runtimeLibraries = runtime.settings.editorTheme.library.sources;
} catch(err) {
runtimeLibraries = [];
}
// userLibraries = runtime.settings.get("library")
} }
function registerType(id,type) { function registerType(id,type) {
// TODO: would like to enforce this, but currently the tests register the same type multiple // TODO: would like to enforce this, but currently the tests register the same type multiple
// times and have no way to remove themselves. // times and have no way to remove themselves.

View File

@ -44,6 +44,11 @@ describe("runtime-api/settings", function() {
paletteCategories :["red","blue","green"], paletteCategories :["red","blue","green"],
exportNodeSettings: (obj) => { exportNodeSettings: (obj) => {
obj.testNodeSetting = "helloWorld"; obj.testNodeSetting = "helloWorld";
},
},
plugins: {
exportPluginSettings: (obj) => {
obj.testPluginSettings = "helloPluginWorld";
} }
}, },
nodes: { nodes: {
@ -51,13 +56,16 @@ describe("runtime-api/settings", function() {
installerEnabled: () => false, installerEnabled: () => false,
getCredentialKeyType: () => "test-key-type" getCredentialKeyType: () => "test-key-type"
}, },
library: {getLibraries: () => ["lib1"] },
storage: {} storage: {}
}) })
return settings.getRuntimeSettings({}).then(result => { return settings.getRuntimeSettings({}).then(result => {
result.should.have.property("httpNodeRoot","testHttpNodeRoot"); result.should.have.property("httpNodeRoot","testHttpNodeRoot");
result.should.have.property("version","testVersion"); result.should.have.property("version","testVersion");
result.should.have.property("paletteCategories",["red","blue","green"]); result.should.have.property("paletteCategories",["red","blue","green"]);
result.should.have.property("libraries",["lib1"]);
result.should.have.property("testNodeSetting","helloWorld"); result.should.have.property("testNodeSetting","helloWorld");
result.should.have.property("testPluginSettings","helloPluginWorld");
result.should.not.have.property("foo",123); result.should.not.have.property("foo",123);
result.should.have.property("flowEncryptionType","test-key-type"); result.should.have.property("flowEncryptionType","test-key-type");
result.should.not.have.property("user"); result.should.not.have.property("user");
@ -75,6 +83,11 @@ describe("runtime-api/settings", function() {
paletteCategories :["red","blue","green"], paletteCategories :["red","blue","green"],
exportNodeSettings: (obj) => { exportNodeSettings: (obj) => {
obj.testNodeSetting = "helloWorld"; obj.testNodeSetting = "helloWorld";
},
},
plugins: {
exportPluginSettings: (obj) => {
obj.testPluginSettings = "helloPluginWorld";
} }
}, },
nodes: { nodes: {
@ -82,6 +95,7 @@ describe("runtime-api/settings", function() {
installerEnabled: () => false, installerEnabled: () => false,
getCredentialKeyType: () => "test-key-type" getCredentialKeyType: () => "test-key-type"
}, },
library: {getLibraries: () => { ["lib1"]} },
storage: {} storage: {}
}) })
return settings.getRuntimeSettings({ return settings.getRuntimeSettings({
@ -111,6 +125,11 @@ describe("runtime-api/settings", function() {
paletteCategories :["red","blue","green"], paletteCategories :["red","blue","green"],
exportNodeSettings: (obj) => { exportNodeSettings: (obj) => {
obj.testNodeSetting = "helloWorld"; obj.testNodeSetting = "helloWorld";
},
},
plugins: {
exportPluginSettings: (obj) => {
obj.testPluginSettings = "helloPluginWorld";
} }
}, },
nodes: { nodes: {
@ -118,6 +137,7 @@ describe("runtime-api/settings", function() {
installerEnabled: () => false, installerEnabled: () => false,
getCredentialKeyType: () => "test-key-type" getCredentialKeyType: () => "test-key-type"
}, },
library: {getLibraries: () => { ["lib1"]} },
storage: { storage: {
projects: { projects: {
getActiveProject: () => 'test-active-project', getActiveProject: () => 'test-active-project',
@ -162,6 +182,11 @@ describe("runtime-api/settings", function() {
paletteCategories :["red","blue","green"], paletteCategories :["red","blue","green"],
exportNodeSettings: (obj) => { exportNodeSettings: (obj) => {
obj.testNodeSetting = "helloWorld"; obj.testNodeSetting = "helloWorld";
},
},
plugins: {
exportPluginSettings: (obj) => {
obj.testPluginSettings = "helloPluginWorld";
} }
}, },
nodes: { nodes: {
@ -169,6 +194,7 @@ describe("runtime-api/settings", function() {
installerEnabled: () => false, installerEnabled: () => false,
getCredentialKeyType: () => "test-key-type" getCredentialKeyType: () => "test-key-type"
}, },
library: {getLibraries: () => { ["lib1"]} },
storage: { storage: {
projects: { projects: {
getActiveProject: () => 'test-active-project', getActiveProject: () => 'test-active-project',
@ -203,6 +229,11 @@ describe("runtime-api/settings", function() {
paletteCategories :["red","blue","green"], paletteCategories :["red","blue","green"],
exportNodeSettings: (obj) => { exportNodeSettings: (obj) => {
obj.testNodeSetting = "helloWorld"; obj.testNodeSetting = "helloWorld";
},
},
plugins: {
exportPluginSettings: (obj) => {
obj.testPluginSettings = "helloPluginWorld";
} }
}, },
nodes: { nodes: {
@ -210,6 +241,7 @@ describe("runtime-api/settings", function() {
installerEnabled: () => false, installerEnabled: () => false,
getCredentialKeyType: () => "test-key-type" getCredentialKeyType: () => "test-key-type"
}, },
library: {getLibraries: () => { ["lib1"]} },
storage: { storage: {
projects: { projects: {
flowFileExists: () => true, flowFileExists: () => true,
@ -248,6 +280,11 @@ describe("runtime-api/settings", function() {
paletteCategories :["red","blue","green"], paletteCategories :["red","blue","green"],
exportNodeSettings: (obj) => { exportNodeSettings: (obj) => {
obj.testNodeSetting = "helloWorld"; obj.testNodeSetting = "helloWorld";
},
},
plugins: {
exportPluginSettings: (obj) => {
obj.testPluginSettings = "helloPluginWorld";
} }
}, },
nodes: { nodes: {
@ -255,6 +292,7 @@ describe("runtime-api/settings", function() {
installerEnabled: () => false, installerEnabled: () => false,
getCredentialKeyType: () => "test-key-type" getCredentialKeyType: () => "test-key-type"
}, },
library: {getLibraries: () => { ["lib1"]} },
storage: { storage: {
projects: { projects: {
flowFileExists: () => false, flowFileExists: () => false,

View File

@ -14,13 +14,14 @@
* limitations under the License. * limitations under the License.
**/ **/
var should = require("should"); const should = require("should");
var sinon = require("sinon"); const sinon = require("sinon");
var NR_TEST_UTILS = require("nr-test-utils"); const NR_TEST_UTILS = require("nr-test-utils");
var library = NR_TEST_UTILS.require("@node-red/runtime/lib/library/index") const library = NR_TEST_UTILS.require("@node-red/runtime/lib/library/index")
var localLibrary = NR_TEST_UTILS.require("@node-red/runtime/lib/library/local") const localLibrary = NR_TEST_UTILS.require("@node-red/runtime/lib/library/local")
var examplesLibrary = NR_TEST_UTILS.require("@node-red/runtime/lib/library/examples") const examplesLibrary = NR_TEST_UTILS.require("@node-red/runtime/lib/library/examples")
const events = NR_TEST_UTILS.require("@node-red/util/lib/events")
var mockLog = { var mockLog = {
log: sinon.stub(), log: sinon.stub(),
@ -72,6 +73,59 @@ describe("runtime/library", function() {
// should(()=>{library.register("unknown","/abc")} ).throw(); // should(()=>{library.register("unknown","/abc")} ).throw();
// }) // })
}) })
describe("getLibraries", function() {
before(function() {
library.init({});
});
it('returns the default and examples libraries', function() {
const libs = library.getLibraries();
libs.should.have.length(2);
libs[0].should.have.property('id', 'local');
libs[0].should.have.property('label','editor:library.types.local');
libs[0].should.have.property("user", false);
libs[0].should.have.property('icon', 'font-awesome/fa-hdd-o');
libs[1].should.have.property('id', 'examples');
libs[1].should.have.property('label','editor:library.types.examples');
libs[1].should.have.property("user", false);
libs[1].should.have.property('icon', 'font-awesome/fa-life-ring');
libs[1].should.have.property('readOnly', true);
libs[1].should.have.property('types', ['flows']);
});
it('returns the libraries from settings', function() {
library.init({
plugins: {
getPlugin: id => { return {
id: "test-library-plugin",
type: "node-red-library-source",
class: function() {}
}
}
},
settings: {
editorTheme: {
library: {
sources: [
{id: "test-plugin-id", type:"test-library-plugin"}
]
}
}
}
});
let libs = library.getLibraries();
libs.should.have.length(2);
events.emit("registry:plugin-added","test-library-plugin" )
libs = library.getLibraries();
libs.should.have.length(3);
libs[2].should.have.property('id', 'test-plugin-id');
libs[2].should.have.property("user", false);
});
})
describe("getEntry", function() { describe("getEntry", function() {
before(function() { before(function() {
library.init({}); library.init({});
@ -102,7 +156,7 @@ describe("runtime/library", function() {
}); });
it ('returns a flow example entry', function(done) { it ('returns a flow example entry', function(done) {
library.getEntry("_examples_","flows","/test-module/abc").then(function(result) { library.getEntry("examples","flows","/test-module/abc").then(function(result) {
result.should.have.property("library","_examples_") result.should.have.property("library","_examples_")
result.should.have.property("path","/test-module/abc") result.should.have.property("path","/test-module/abc")
done(); done();