liudong
2023-05-29 340f156319b863525e50e900c58e59b86ecb3d5e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
 
const should = require("should");
const sinon = require("sinon");
const path = require("path");
 
const NR_TEST_UTILS = require("nr-test-utils");
 
const plugins = NR_TEST_UTILS.require("@node-red/registry/lib/plugins");
const registry = NR_TEST_UTILS.require("@node-red/registry/lib/registry");
const { events } = NR_TEST_UTILS.require("@node-red/util");
 
describe("red/nodes/registry/plugins",function() {
    let receivedEvents = [];
    let modules;
    function handleEvent(evnt) {
        receivedEvents.push(evnt);
    }
    beforeEach(function() {
        plugins.init({});
        receivedEvents = [];
        modules = {
            "test-module": {
                plugins: {
                    "test-set": {
                        id: "test-module/test-set",
                        enabled: true,
                        config: "test-module-config",
                        plugins: []
                    },
                    "test-disabled-set": {
                        id: "test-module/test-disabled-set",
                        enabled: false,
                        config: "disabled-plugin-config",
                        plugins: []
                    }
                }
            }
        }
        events.on("registry:plugin-added",handleEvent);
        sinon.stub(registry,"getModule").callsFake(moduleId => modules[moduleId]);
        sinon.stub(registry,"getModuleList").callsFake(() => modules)
    });
    afterEach(function() {
        events.removeListener("registry:plugin-added",handleEvent);
        registry.getModule.restore();
        registry.getModuleList.restore();
    })
 
    describe("registerPlugin", function() {
        it("registers a plugin", function() {
            let pluginDef = {}
            plugins.registerPlugin("test-module/test-set","a-plugin",pluginDef);
            receivedEvents.length.should.eql(1);
            receivedEvents[0].should.eql("a-plugin");
            should.exist(modules['test-module'].plugins['test-set'].plugins[0])
            modules['test-module'].plugins['test-set'].plugins[0].should.equal(pluginDef)
        })
        it("calls a plugins onadd function", function() {
            let pluginDef = { onadd: sinon.stub() }
            plugins.registerPlugin("test-module/test-set","a-plugin",pluginDef);
            pluginDef.onadd.called.should.be.true();
        })
    })
 
    describe("getPlugin", function() {
        it("returns a registered plugin", function() {
            let pluginDef = {}
            plugins.registerPlugin("test-module/test-set","a-plugin",pluginDef);
            pluginDef.should.equal(plugins.getPlugin("a-plugin"));
        })
    })
    describe("getPluginsByType", function() {
        it("returns a plugins of a given type", function() {
            let pluginDef = {type: "foo"}
            let pluginDef2 = {type: "bar"}
            let pluginDef3 = {type: "foo"}
            plugins.registerPlugin("test-module/test-set","a-plugin",pluginDef);
            plugins.registerPlugin("test-module/test-set","a-plugin2",pluginDef2);
            plugins.registerPlugin("test-module/test-set","a-plugin3",pluginDef3);
 
            let fooPlugins = plugins.getPluginsByType("foo");
            let barPlugins = plugins.getPluginsByType("bar");
            let noPlugins = plugins.getPluginsByType("none");
 
            noPlugins.should.be.of.length(0);
 
            fooPlugins.should.be.of.length(2);
            fooPlugins.should.containEql(pluginDef);
            fooPlugins.should.containEql(pluginDef3);
 
            barPlugins.should.be.of.length(1);
            barPlugins.should.containEql(pluginDef2);
 
        })
    })
 
    describe("getPluginConfigs", function() {
        it("gets all plugin configs", function() {
            let configs = plugins.getPluginConfigs("en-US");
            configs.should.eql(`
<!-- --- [red-plugin:test-module/test-set] --- -->
test-module-config`)
        })
    })
 
 
    describe("getPluginList", function() {
        it("returns a plugins of a given type", function() {
            let pluginDef = {type: "foo"}
            let pluginDef2 = {type: "bar"}
            let pluginDef3 = {type: "foo"}
            plugins.registerPlugin("test-module/test-set","a-plugin",pluginDef);
            plugins.registerPlugin("test-module/test-set","a-plugin2",pluginDef2);
            plugins.registerPlugin("test-module/test-set","a-plugin3",pluginDef3);
 
            let pluginList = plugins.getPluginList();
            JSON.stringify(pluginList).should.eql(JSON.stringify(
                [
                    {
                        "id": "test-module/test-set",
                        "enabled": true,
                        "local": false,
                        "user": false,
                        "plugins": [
                            {
                                "type": "foo",
                                "id": "a-plugin",
                                "module": "test-module"
                            },
                            {
                                "type": "bar",
                                "id": "a-plugin2",
                                "module": "test-module"
                            },
                            {
                                "type": "foo",
                                "id": "a-plugin3",
                                "module": "test-module"
                            }
                        ]
                    },
                    {
                        "id": "test-module/test-disabled-set",
                        "enabled": false,
                        "local": false,
                        "user": false,
                        "plugins": []
                    }
                ]
            ))
        })
    })
    describe("exportPluginSettings", function() {
        it("exports plugin settings - default false", function() {
            plugins.init({ "a-plugin": { a: 123, b:234, c: 345} });
            plugins.registerPlugin("test-module/test-set","a-plugin",{
                settings: {
                    a: { exportable: true },
                    b: {exportable: false },
                    d: { exportable: true, value: 456}
 
                }
            });
            var exportedSet = {};
            plugins.exportPluginSettings(exportedSet);
            exportedSet.should.have.property("a-plugin");
            // a is exportable
            exportedSet["a-plugin"].should.have.property("a",123);
            // b is explicitly not exportable
            exportedSet["a-plugin"].should.not.have.property("b");
            // c isn't listed and default false
            exportedSet["a-plugin"].should.not.have.property("c");
            // d has a default value
            exportedSet["a-plugin"].should.have.property("d",456);
        })
        it("exports plugin settings - default true", function() {
            plugins.init({ "a-plugin": { a: 123, b:234, c: 345} });
            plugins.registerPlugin("test-module/test-set","a-plugin",{
                settings: {
                    '*': { exportable: true },
                    a: { exportable: true },
                    b: {exportable: false },
                    d: { exportable: true, value: 456}
 
                }
            });
            var exportedSet = {};
            plugins.exportPluginSettings(exportedSet);
            exportedSet.should.have.property("a-plugin");
            // a is exportable
            exportedSet["a-plugin"].should.have.property("a",123);
            // b is explicitly not exportable
            exportedSet["a-plugin"].should.not.have.property("b");
            // c isn't listed, but default true
            exportedSet["a-plugin"].should.have.property("c");
            // d has a default value
            exportedSet["a-plugin"].should.have.property("d",456);
        })
    });
});