Skip to content

Commit 24a4c3d

Browse files
author
Marc Stirner
committed
Merge branch 'dev' of github.com:mStirner/backend into dev
2 parents ff50f3d + 163c099 commit 24a4c3d

File tree

11 files changed

+290
-25
lines changed

11 files changed

+290
-25
lines changed

components/endpoints/class.command.js

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ const { interfaces } = require("../../system/shared.js");
1919
* @property {String} alias Machine friendly name, e.g.: `POWER_ON`
2020
* @property {String} [identifier=null] Simple/custom identifiert for custom command handler
2121
* @property {String} payload The payload to send over the device interface
22-
* @property {String} [description=""] Command description, displayed on the frontend
22+
* @property {String} [description=null] Command description, displayed on the frontend
2323
* @property {Array} params Possible parameter for the command
2424
* @property {String} params[].key Custom key
25-
* @property {Any} params[].value Value to set
26-
* @property {String} params[].default Default thing if nothing is send from client
27-
* @property {String} params[].min Min value if param type is a number
28-
* @property {String} params[].max Max value if param type is a number
25+
* @property {String} params[].type Type of value: "string", "number" or "boolean"
26+
* @property {String|Number|Boolean} params[].value Value to set
27+
* @property {Number} [params[].min=0] Min value if param type is a number (`type=number`)
28+
* @property {Number} [params[].max=100] Max value if param type is a number (`type=number`)
2929
*
3030
* @example
3131
* ```json
@@ -223,13 +223,29 @@ module.exports = class Command {
223223
identifier: Joi.string().allow(null).default(null), // NOTE: move to endpoint schema? // Thing api provides you, like light id or some custom thing for you
224224
payload: Joi.string().allow(null).default(null),
225225
description: Joi.string().allow(null).default(null),
226-
params: Joi.array().items({
227-
key: Joi.string().required(),
228-
value: Joi.any(),
229-
default: Joi.string(),
230-
min: Joi.number(),
231-
max: Joi.number()
232-
})
226+
params: Joi.array().items(Joi.object({
227+
type: Joi.string().valid("number", "string", "boolean").required(),
228+
key: Joi.string().required()
229+
}).when(".type", {
230+
switch: [{
231+
is: "number",
232+
then: Joi.object({
233+
value: Joi.number().default(null).allow(null),
234+
min: Joi.number().default(0),
235+
max: Joi.number().default(100)
236+
})
237+
}, {
238+
is: "string",
239+
then: Joi.object({
240+
value: Joi.string().default(null).allow(null)
241+
})
242+
}, {
243+
is: "boolean",
244+
then: Joi.object({
245+
value: Joi.boolean().default(null).allow(null)
246+
})
247+
}]
248+
}))
233249
});
234250
}
235251

components/vault/index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,28 @@ class C_VAULT extends COMPONENT {
7070
next(null, new Vault(data, this));
7171
});
7272

73+
/*
74+
// investigation of #208
75+
this.hooks.post("update", (data, next) => {
76+
77+
console.log("update post:", data);
78+
79+
let valid = data.secrets.every((secret) => {
80+
return secret instanceof Secret;
81+
});
82+
83+
console.log("Secrets instances valid:", valid, data.secrets)
84+
85+
if (!valid) {
86+
//_merge(item, new Vault(data, this));
87+
//Object.assign(data, new Vault(data, this));
88+
}
89+
90+
next(null);
91+
92+
});
93+
*/
94+
7395
}
7496

7597
}

tests/components/devices.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,31 @@ try {
9292
});
9393

9494

95+
workflow(C_COMPONENT, "update", "Double update result / event arguments check", (done, { event }) => {
96+
Promise.all([
97+
98+
// update call 1
99+
C_COMPONENT.update(_id, {
100+
name: "Device #2",
101+
}),
102+
103+
// update call 2
104+
C_COMPONENT.update(_id, {
105+
enabled: true
106+
})
107+
108+
]).then(() => {
109+
110+
event.args.forEach((args) => {
111+
assert.equal(args[0] instanceof Device, true);
112+
});
113+
114+
done();
115+
116+
}).catch(done);
117+
});
118+
119+
95120
workflow(C_COMPONENT, "remove", (done, { post }) => {
96121
C_COMPONENT.remove(_id, (err, item) => {
97122
try {

tests/components/endpoints.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,31 @@ try {
7878
});
7979

8080

81+
workflow(C_COMPONENT, "update", "Double update result / event arguments check", (done, { event }) => {
82+
Promise.all([
83+
84+
// update call 1
85+
C_COMPONENT.update(_id, {
86+
name: "Endpoint #42",
87+
}),
88+
89+
// update call 2
90+
C_COMPONENT.update(_id, {
91+
enabled: false
92+
})
93+
94+
]).then(() => {
95+
96+
event.args.forEach((args) => {
97+
assert.equal(args[0] instanceof Endpoint, true);
98+
});
99+
100+
done();
101+
102+
}).catch(done);
103+
});
104+
105+
81106
workflow(C_COMPONENT, "remove", (done, { post }) => {
82107
C_COMPONENT.remove(_id, (err, item) => {
83108
try {

tests/components/plugins.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,31 @@ try {
7878
});
7979

8080

81+
workflow(C_COMPONENT, "update", "Double update result / event arguments check", (done, { event }) => {
82+
Promise.all([
83+
84+
// update call 1
85+
C_COMPONENT.update(_id, {
86+
version: 3
87+
}),
88+
89+
// update call 2
90+
C_COMPONENT.update(_id, {
91+
enabled: false
92+
})
93+
94+
]).then(() => {
95+
96+
event.args.forEach((args) => {
97+
assert.equal(args[0] instanceof Plugin, true);
98+
});
99+
100+
done();
101+
102+
}).catch(done);
103+
});
104+
105+
81106
workflow(C_COMPONENT, "remove", (done, { post }) => {
82107
C_COMPONENT.remove(_id, (err, item) => {
83108
try {

tests/components/rooms.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,33 @@ try {
7878
});
7979

8080

81+
workflow(C_COMPONENT, "update", "Double update result / event arguments check", (done, { event }) => {
82+
Promise.all([
83+
84+
// update call 1
85+
C_COMPONENT.update(_id, {
86+
name: "New name",
87+
floor: 2
88+
}),
89+
90+
// update call 2
91+
C_COMPONENT.update(_id, {
92+
floor: 9,
93+
number: 42069
94+
})
95+
96+
]).then(() => {
97+
98+
event.args.forEach((args) => {
99+
assert.equal(args[0] instanceof Room, true);
100+
});
101+
102+
done();
103+
104+
}).catch(done);
105+
});
106+
107+
81108
workflow(C_COMPONENT, "remove", (done, { post }) => {
82109
C_COMPONENT.remove(_id, (err, item) => {
83110
try {

tests/components/ssdp.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const assert = require("assert");
2+
const crypto = require("crypto");
23
const mongodb = require("mongodb");
34

45
try {
@@ -75,6 +76,35 @@ try {
7576
});
7677

7778

79+
workflow(C_COMPONENT, "update", "Double update result / event arguments check", (done, { event }) => {
80+
81+
let uuid1 = crypto.randomUUID();
82+
let uuid2 = crypto.randomUUID();
83+
84+
Promise.all([
85+
86+
// update call 1
87+
C_COMPONENT.update(_id, {
88+
usn: `uuid:${uuid1}`
89+
}),
90+
91+
// update call 2
92+
C_COMPONENT.update(_id, {
93+
usn: `uuid:${uuid2}`
94+
})
95+
96+
]).then(() => {
97+
98+
event.args.forEach((args) => {
99+
assert.equal(args[0] instanceof SSDP, true);
100+
});
101+
102+
done();
103+
104+
}).catch(done);
105+
});
106+
107+
78108
workflow(C_COMPONENT, "remove", (done, { post }) => {
79109
C_COMPONENT.remove(_id, (err, item) => {
80110
try {

tests/components/store.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,31 @@ try {
8080
});
8181

8282

83+
workflow(C_COMPONENT, "update", "Double update result / event arguments check", (done, { event }) => {
84+
Promise.all([
85+
86+
// update call 1
87+
C_COMPONENT.update(_id, {
88+
item: uuidv4()
89+
}),
90+
91+
// update call 2
92+
C_COMPONENT.update(_id, {
93+
item: uuidv4()
94+
})
95+
96+
]).then(() => {
97+
98+
event.args.forEach((args) => {
99+
assert.equal(args[0] instanceof Store, true);
100+
});
101+
102+
done();
103+
104+
}).catch(done);
105+
});
106+
107+
83108
workflow(C_COMPONENT, "remove", (done, { post }) => {
84109
C_COMPONENT.remove(_id, (err, item) => {
85110
try {

tests/components/test.workflow.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ const sinon = require("sinon");
44

55
const _iterate = require("../../helper/iterate.js");
66

7-
module.exports = (C_COMPONENT, method, worker) => {
7+
module.exports = (C_COMPONENT, method, desc, worker) => {
8+
9+
if (!worker && desc instanceof Function) {
10+
worker = desc;
11+
desc = null;
12+
}
813

914
let pre = sinon.spy();
1015
let post = sinon.spy();
@@ -20,31 +25,33 @@ module.exports = (C_COMPONENT, method, worker) => {
2025
args[args.length - 1](null);
2126
});
2227

23-
C_COMPONENT.events.once(method, (...args) => {
28+
C_COMPONENT.events.on(method, (...args) => {
29+
//console.log("Event", method, args);
2430
event(...args);
2531
});
2632

27-
it(`Should perform method "${method}"`, (done) => {
33+
it(`Should perform method "${method}" ${desc ? "(" + desc + ")" : ""}`, (done) => {
2834
worker(done, {
2935
pre,
3036
post,
3137
event
3238
});
3339
});
3440

35-
it(`Should fired event "${method}"`, (done) => {
36-
assert.equal(event.calledOnce, true);
41+
it(`Should fired event "${method}" at least once ${desc ? "(" + desc + ")" : ""}`, (done) => {
42+
//assert.equal(event.calledOnce, true);
43+
assert.ok(event.callCount >= 1);
3744
done();
3845
});
3946

4047
if (method === "add") {
4148

42-
it(`Items array should have 1 item`, (done) => {
49+
it(`Items array should have 1 item ${desc ? "(" + desc + ")" : ""}`, (done) => {
4350
assert.equal(C_COMPONENT.items.length, 1);
4451
done();
4552
});
4653

47-
it(`Every array in item should have a _id property`, (done) => {
54+
it(`Every array in item should have a _id property ${desc ? "(" + desc + ")" : ""}`, (done) => {
4855
try {
4956

5057
_iterate(C_COMPONENT.items[0], (key, value, type) => {
@@ -78,7 +85,7 @@ module.exports = (C_COMPONENT, method, worker) => {
7885

7986
}
8087

81-
it(`Should fire pre hook "${method}"`, (done) => {
88+
it(`Should fire pre hook "${method}" ${desc ? "(" + desc + ")" : ""}`, (done) => {
8289
try {
8390

8491
// NOTE:
@@ -97,7 +104,7 @@ module.exports = (C_COMPONENT, method, worker) => {
97104
}
98105
});
99106

100-
it(`Should fire post hook "${method}"`, (done) => {
107+
it(`Should fire post hook "${method}" ${desc ? "(" + desc + ")" : ""}`, (done) => {
101108
try {
102109

103110
if (method === "remove") {

tests/components/users.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,31 @@ try {
7878
});
7979

8080

81+
workflow(C_COMPONENT, "update", "Double update result / event arguments check", (done, { event }) => {
82+
Promise.all([
83+
84+
// update call 1
85+
C_COMPONENT.update(_id, {
86+
password: "12345678"
87+
}),
88+
89+
// update call 2
90+
C_COMPONENT.update(_id, {
91+
enabled: true
92+
})
93+
94+
]).then(() => {
95+
96+
event.args.forEach((args) => {
97+
assert.equal(args[0] instanceof User, true);
98+
});
99+
100+
done();
101+
102+
}).catch(done);
103+
});
104+
105+
81106
workflow(C_COMPONENT, "remove", (done, { post }) => {
82107
C_COMPONENT.remove(_id, (err, item) => {
83108
try {

0 commit comments

Comments
 (0)