@iobroker/testing 5.0.0 → 5.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +1 -1
- package/build/index.d.ts +1 -1
- package/build/lib/adapterTools.d.ts +7 -1
- package/build/lib/adapterTools.js +36 -21
- package/build/lib/executeCommand.d.ts +3 -2
- package/build/lib/executeCommand.js +30 -29
- package/build/lib/str2regex.js +4 -4
- package/build/tests/index.d.ts +9 -9
- package/build/tests/integration/index.d.ts +1 -1
- package/build/tests/integration/index.js +44 -39
- package/build/tests/integration/lib/adapterSetup.d.ts +1 -1
- package/build/tests/integration/lib/adapterSetup.js +50 -50
- package/build/tests/integration/lib/controllerSetup.d.ts +1 -10
- package/build/tests/integration/lib/controllerSetup.js +59 -56
- package/build/tests/integration/lib/dbConnection.d.ts +12 -12
- package/build/tests/integration/lib/dbConnection.js +71 -69
- package/build/tests/integration/lib/harness.d.ts +9 -7
- package/build/tests/integration/lib/harness.js +68 -49
- package/build/tests/integration/lib/logger.js +1 -1
- package/build/tests/integration/lib/tools.d.ts +6 -1
- package/build/tests/integration/lib/tools.js +27 -12
- package/build/tests/packageFiles/index.js +92 -90
- package/build/tests/unit/harness/createMocks.d.ts +3 -2
- package/build/tests/unit/harness/createMocks.js +0 -1
- package/build/tests/unit/index.js +5 -4
- package/build/tests/unit/mocks/mockAdapter.d.ts +3 -3
- package/build/tests/unit/mocks/mockAdapter.js +196 -162
- package/build/tests/unit/mocks/mockAdapterCore.d.ts +4 -9
- package/build/tests/unit/mocks/mockAdapterCore.js +23 -14
- package/build/tests/unit/mocks/mockDatabase.d.ts +13 -12
- package/build/tests/unit/mocks/mockDatabase.js +39 -43
- package/build/tests/unit/mocks/mockLogger.d.ts +1 -1
- package/build/tests/unit/mocks/mockLogger.js +1 -1
- package/build/tests/unit/mocks/tools.d.ts +2 -2
- package/build/tests/unit/mocks/tools.js +14 -11
- package/package.json +15 -22
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createAdapterMock = createAdapterMock;
|
|
4
|
+
// @ts-expect-error no types
|
|
4
5
|
const objects_1 = require("alcalzone-shared/objects");
|
|
5
6
|
const sinon_1 = require("sinon");
|
|
6
7
|
const mockLogger_1 = require("./mockLogger");
|
|
@@ -8,80 +9,82 @@ const tools_1 = require("./tools");
|
|
|
8
9
|
// Define here which methods were implemented manually, so we can hook them up with a real stub
|
|
9
10
|
// The value describes if and how the async version of the callback is constructed
|
|
10
11
|
const implementedMethods = {
|
|
11
|
-
getObject:
|
|
12
|
-
setObject:
|
|
13
|
-
setObjectNotExists:
|
|
14
|
-
extendObject:
|
|
15
|
-
getForeignObject:
|
|
16
|
-
getForeignObjects:
|
|
17
|
-
setForeignObject:
|
|
18
|
-
setForeignObjectNotExists:
|
|
19
|
-
extendForeignObject:
|
|
20
|
-
getState:
|
|
21
|
-
getStates:
|
|
22
|
-
setState:
|
|
23
|
-
setStateChanged:
|
|
24
|
-
delState:
|
|
25
|
-
getForeignState:
|
|
26
|
-
setForeignState:
|
|
27
|
-
setForeignStateChanged:
|
|
28
|
-
subscribeStates:
|
|
29
|
-
subscribeForeignStates:
|
|
30
|
-
subscribeObjects:
|
|
31
|
-
subscribeForeignObjects:
|
|
32
|
-
getAdapterObjects:
|
|
33
|
-
getObjectView:
|
|
34
|
-
getObjectList:
|
|
35
|
-
on:
|
|
36
|
-
removeListener:
|
|
37
|
-
removeAllListeners:
|
|
38
|
-
terminate:
|
|
39
|
-
getPort:
|
|
40
|
-
checkPassword:
|
|
41
|
-
setPassword:
|
|
42
|
-
checkGroup:
|
|
43
|
-
calculatePermissions:
|
|
44
|
-
getCertificates:
|
|
45
|
-
sendTo:
|
|
46
|
-
sendToHost:
|
|
47
|
-
getHistory:
|
|
12
|
+
getObject: 'normal',
|
|
13
|
+
setObject: 'normal',
|
|
14
|
+
setObjectNotExists: 'normal',
|
|
15
|
+
extendObject: 'normal',
|
|
16
|
+
getForeignObject: 'normal',
|
|
17
|
+
getForeignObjects: 'normal',
|
|
18
|
+
setForeignObject: 'normal',
|
|
19
|
+
setForeignObjectNotExists: 'normal',
|
|
20
|
+
extendForeignObject: 'normal',
|
|
21
|
+
getState: 'normal',
|
|
22
|
+
getStates: 'normal',
|
|
23
|
+
setState: 'normal',
|
|
24
|
+
setStateChanged: 'normal',
|
|
25
|
+
delState: 'normal',
|
|
26
|
+
getForeignState: 'normal',
|
|
27
|
+
setForeignState: 'normal',
|
|
28
|
+
setForeignStateChanged: 'normal',
|
|
29
|
+
subscribeStates: 'normal',
|
|
30
|
+
subscribeForeignStates: 'normal',
|
|
31
|
+
subscribeObjects: 'normal',
|
|
32
|
+
subscribeForeignObjects: 'normal',
|
|
33
|
+
getAdapterObjects: 'no error',
|
|
34
|
+
getObjectView: 'normal',
|
|
35
|
+
getObjectList: 'normal',
|
|
36
|
+
on: 'none',
|
|
37
|
+
removeListener: 'none',
|
|
38
|
+
removeAllListeners: 'none',
|
|
39
|
+
terminate: 'none',
|
|
40
|
+
getPort: 'no error',
|
|
41
|
+
checkPassword: 'no error',
|
|
42
|
+
setPassword: 'normal',
|
|
43
|
+
checkGroup: 'no error',
|
|
44
|
+
calculatePermissions: 'no error',
|
|
45
|
+
getCertificates: 'normal',
|
|
46
|
+
sendTo: 'no error',
|
|
47
|
+
sendToHost: 'no error',
|
|
48
|
+
getHistory: 'normal',
|
|
48
49
|
// @ts-expect-error This method was deprecated
|
|
49
|
-
setBinaryState:
|
|
50
|
-
getBinaryState:
|
|
51
|
-
getEnum:
|
|
52
|
-
getEnums:
|
|
53
|
-
addChannelToEnum:
|
|
54
|
-
deleteChannelFromEnum:
|
|
55
|
-
addStateToEnum:
|
|
56
|
-
deleteStateFromEnum:
|
|
57
|
-
createDevice:
|
|
58
|
-
deleteDevice:
|
|
59
|
-
createChannel:
|
|
60
|
-
deleteChannel:
|
|
61
|
-
createState:
|
|
62
|
-
deleteState:
|
|
63
|
-
getDevices:
|
|
64
|
-
getChannelsOf:
|
|
65
|
-
getStatesOf:
|
|
66
|
-
readDir:
|
|
67
|
-
mkDir:
|
|
68
|
-
readFile:
|
|
69
|
-
writeFile:
|
|
70
|
-
delFile:
|
|
71
|
-
unlink:
|
|
72
|
-
rename:
|
|
73
|
-
chmodFile:
|
|
50
|
+
setBinaryState: 'normal',
|
|
51
|
+
getBinaryState: 'normal',
|
|
52
|
+
getEnum: 'normal',
|
|
53
|
+
getEnums: 'normal',
|
|
54
|
+
addChannelToEnum: 'normal',
|
|
55
|
+
deleteChannelFromEnum: 'normal',
|
|
56
|
+
addStateToEnum: 'normal',
|
|
57
|
+
deleteStateFromEnum: 'normal',
|
|
58
|
+
createDevice: 'normal',
|
|
59
|
+
deleteDevice: 'normal',
|
|
60
|
+
createChannel: 'normal',
|
|
61
|
+
deleteChannel: 'normal',
|
|
62
|
+
createState: 'normal',
|
|
63
|
+
deleteState: 'normal',
|
|
64
|
+
getDevices: 'normal',
|
|
65
|
+
getChannelsOf: 'normal',
|
|
66
|
+
getStatesOf: 'normal',
|
|
67
|
+
readDir: 'normal',
|
|
68
|
+
mkDir: 'normal',
|
|
69
|
+
readFile: 'normal',
|
|
70
|
+
writeFile: 'normal',
|
|
71
|
+
delFile: 'normal',
|
|
72
|
+
unlink: 'normal',
|
|
73
|
+
rename: 'normal',
|
|
74
|
+
chmodFile: 'normal',
|
|
74
75
|
};
|
|
75
76
|
function getCallback(...args) {
|
|
76
77
|
const lastArg = args[args.length - 1];
|
|
77
|
-
if (typeof lastArg ===
|
|
78
|
+
if (typeof lastArg === 'function') {
|
|
78
79
|
return lastArg;
|
|
80
|
+
}
|
|
79
81
|
}
|
|
80
82
|
/** Stub implementation which can be promisified */
|
|
81
83
|
const asyncEnabledStub = ((...args) => {
|
|
82
84
|
const callback = getCallback(...args);
|
|
83
|
-
if (typeof callback ===
|
|
85
|
+
if (typeof callback === 'function') {
|
|
84
86
|
callback();
|
|
87
|
+
}
|
|
85
88
|
});
|
|
86
89
|
/**
|
|
87
90
|
* Creates an adapter mock that is connected to a given database mock
|
|
@@ -90,18 +93,18 @@ function createAdapterMock(db, options = {}) {
|
|
|
90
93
|
// In order to support ES6-style adapters with inheritance, we need to work on the instance directly
|
|
91
94
|
const ret = this || {};
|
|
92
95
|
Object.assign(ret, {
|
|
93
|
-
name: options.name ||
|
|
94
|
-
host:
|
|
96
|
+
name: options.name || 'test',
|
|
97
|
+
host: 'testhost',
|
|
95
98
|
instance: options.instance || 0,
|
|
96
|
-
namespace: `${options.name ||
|
|
99
|
+
namespace: `${options.name || 'test'}.${options.instance || 0}`,
|
|
97
100
|
config: options.config || {},
|
|
98
101
|
common: {},
|
|
99
102
|
systemConfig: null,
|
|
100
|
-
adapterDir:
|
|
103
|
+
adapterDir: '',
|
|
101
104
|
ioPack: {},
|
|
102
105
|
pack: {},
|
|
103
106
|
log: (0, mockLogger_1.createLoggerMock)(),
|
|
104
|
-
version:
|
|
107
|
+
version: 'any',
|
|
105
108
|
connected: true,
|
|
106
109
|
getPort: asyncEnabledStub,
|
|
107
110
|
stop: (0, sinon_1.stub)(),
|
|
@@ -114,28 +117,34 @@ function createAdapterMock(db, options = {}) {
|
|
|
114
117
|
sendToHost: asyncEnabledStub,
|
|
115
118
|
idToDCS: (0, sinon_1.stub)(),
|
|
116
119
|
getObject: ((id, ...args) => {
|
|
117
|
-
if (!id.startsWith(ret.namespace))
|
|
118
|
-
id = ret.namespace
|
|
120
|
+
if (!id.startsWith(ret.namespace)) {
|
|
121
|
+
id = `${ret.namespace}.${id}`;
|
|
122
|
+
}
|
|
119
123
|
const callback = getCallback(...args);
|
|
120
|
-
if (callback)
|
|
124
|
+
if (callback) {
|
|
121
125
|
callback(null, db.getObject(id));
|
|
126
|
+
}
|
|
122
127
|
}),
|
|
123
128
|
setObject: ((id, obj, ...args) => {
|
|
124
|
-
if (!id.startsWith(ret.namespace))
|
|
125
|
-
id = ret.namespace
|
|
129
|
+
if (!id.startsWith(ret.namespace)) {
|
|
130
|
+
id = `${ret.namespace}.${id}`;
|
|
131
|
+
}
|
|
126
132
|
obj._id = id;
|
|
127
133
|
db.publishObject(obj);
|
|
128
134
|
const callback = getCallback(...args);
|
|
129
|
-
if (callback)
|
|
135
|
+
if (callback) {
|
|
130
136
|
callback(null, { id });
|
|
137
|
+
}
|
|
131
138
|
}),
|
|
132
139
|
setObjectNotExists: ((id, obj, ...args) => {
|
|
133
|
-
if (!id.startsWith(ret.namespace))
|
|
134
|
-
id = ret.namespace
|
|
140
|
+
if (!id.startsWith(ret.namespace)) {
|
|
141
|
+
id = `${ret.namespace}.${id}`;
|
|
142
|
+
}
|
|
135
143
|
const callback = getCallback(...args);
|
|
136
144
|
if (db.hasObject(id)) {
|
|
137
|
-
if (callback)
|
|
145
|
+
if (callback) {
|
|
138
146
|
callback(null, { id });
|
|
147
|
+
}
|
|
139
148
|
}
|
|
140
149
|
else {
|
|
141
150
|
ret.setObject(id, obj, callback);
|
|
@@ -145,19 +154,21 @@ function createAdapterMock(db, options = {}) {
|
|
|
145
154
|
callback(db.getObjects(`${ret.namespace}.*`));
|
|
146
155
|
}),
|
|
147
156
|
getObjectView: ((design, search, { startkey, endkey }, ...args) => {
|
|
148
|
-
if (design !==
|
|
149
|
-
throw new Error(
|
|
157
|
+
if (design !== 'system') {
|
|
158
|
+
throw new Error('If you want to use a custom design for getObjectView, you need to mock it yourself!');
|
|
150
159
|
}
|
|
151
160
|
const callback = getCallback(...args);
|
|
152
|
-
if (typeof callback ===
|
|
153
|
-
let objects = (0, objects_1.values)(db.getObjects(
|
|
154
|
-
objects = objects.filter(
|
|
155
|
-
if (startkey)
|
|
156
|
-
objects = objects.filter(
|
|
157
|
-
|
|
158
|
-
|
|
161
|
+
if (typeof callback === 'function') {
|
|
162
|
+
let objects = (0, objects_1.values)(db.getObjects('*'));
|
|
163
|
+
objects = objects.filter(obj => obj.type === search);
|
|
164
|
+
if (startkey) {
|
|
165
|
+
objects = objects.filter(obj => obj._id >= startkey);
|
|
166
|
+
}
|
|
167
|
+
if (endkey) {
|
|
168
|
+
objects = objects.filter(obj => obj._id <= endkey);
|
|
169
|
+
}
|
|
159
170
|
callback(null, {
|
|
160
|
-
rows: objects.map(
|
|
171
|
+
rows: objects.map(obj => ({
|
|
161
172
|
id: obj._id,
|
|
162
173
|
value: obj,
|
|
163
174
|
})),
|
|
@@ -166,16 +177,19 @@ function createAdapterMock(db, options = {}) {
|
|
|
166
177
|
}),
|
|
167
178
|
getObjectList: (({ startkey, endkey, include_docs, }, ...args) => {
|
|
168
179
|
const callback = getCallback(...args);
|
|
169
|
-
if (typeof callback ===
|
|
170
|
-
let objects = (0, objects_1.values)(db.getObjects(
|
|
171
|
-
if (startkey)
|
|
172
|
-
objects = objects.filter(
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
180
|
+
if (typeof callback === 'function') {
|
|
181
|
+
let objects = (0, objects_1.values)(db.getObjects('*'));
|
|
182
|
+
if (startkey) {
|
|
183
|
+
objects = objects.filter(obj => obj._id >= startkey);
|
|
184
|
+
}
|
|
185
|
+
if (endkey) {
|
|
186
|
+
objects = objects.filter(obj => obj._id <= endkey);
|
|
187
|
+
}
|
|
188
|
+
if (!include_docs) {
|
|
189
|
+
objects = objects.filter(obj => !obj._id.startsWith('_'));
|
|
190
|
+
}
|
|
177
191
|
callback(null, {
|
|
178
|
-
rows: objects.map(
|
|
192
|
+
rows: objects.map(obj => ({
|
|
179
193
|
id: obj._id,
|
|
180
194
|
value: obj,
|
|
181
195
|
doc: obj,
|
|
@@ -184,49 +198,55 @@ function createAdapterMock(db, options = {}) {
|
|
|
184
198
|
}
|
|
185
199
|
}),
|
|
186
200
|
extendObject: ((id, obj, ...args) => {
|
|
187
|
-
if (!id.startsWith(ret.namespace))
|
|
188
|
-
id = ret.namespace
|
|
201
|
+
if (!id.startsWith(ret.namespace)) {
|
|
202
|
+
id = `${ret.namespace}.${id}`;
|
|
203
|
+
}
|
|
189
204
|
const existing = db.getObject(id) || {};
|
|
190
205
|
const target = (0, objects_1.extend)({}, existing, obj);
|
|
191
206
|
target._id = id;
|
|
192
207
|
db.publishObject(target);
|
|
193
208
|
const callback = getCallback(...args);
|
|
194
|
-
if (callback)
|
|
209
|
+
if (callback) {
|
|
195
210
|
callback(null, { id: target._id, value: target }, id);
|
|
211
|
+
}
|
|
196
212
|
}),
|
|
197
213
|
delObject: ((id, ...args) => {
|
|
198
|
-
if (!id.startsWith(ret.namespace))
|
|
199
|
-
id = ret.namespace
|
|
214
|
+
if (!id.startsWith(ret.namespace)) {
|
|
215
|
+
id = `${ret.namespace}.${id}`;
|
|
216
|
+
}
|
|
200
217
|
db.deleteObject(id);
|
|
201
218
|
const callback = getCallback(...args);
|
|
202
|
-
if (callback)
|
|
219
|
+
if (callback) {
|
|
203
220
|
callback(undefined);
|
|
221
|
+
}
|
|
204
222
|
}),
|
|
205
223
|
getForeignObject: ((id, ...args) => {
|
|
206
224
|
const callback = getCallback(...args);
|
|
207
|
-
if (callback)
|
|
225
|
+
if (callback) {
|
|
208
226
|
callback(null, db.getObject(id));
|
|
227
|
+
}
|
|
209
228
|
}),
|
|
210
229
|
getForeignObjects: ((pattern, ...args) => {
|
|
211
|
-
const type = typeof args[0] ===
|
|
212
|
-
? args[0]
|
|
213
|
-
: undefined;
|
|
230
|
+
const type = typeof args[0] === 'string' ? args[0] : undefined;
|
|
214
231
|
const callback = getCallback(...args);
|
|
215
|
-
if (callback)
|
|
232
|
+
if (callback) {
|
|
216
233
|
callback(null, db.getObjects(pattern, type));
|
|
234
|
+
}
|
|
217
235
|
}),
|
|
218
236
|
setForeignObject: ((id, obj, ...args) => {
|
|
219
237
|
obj._id = id;
|
|
220
238
|
db.publishObject(obj);
|
|
221
239
|
const callback = getCallback(...args);
|
|
222
|
-
if (callback)
|
|
240
|
+
if (callback) {
|
|
223
241
|
callback(null, { id });
|
|
242
|
+
}
|
|
224
243
|
}),
|
|
225
244
|
setForeignObjectNotExists: ((id, obj, ...args) => {
|
|
226
245
|
const callback = getCallback(...args);
|
|
227
246
|
if (db.hasObject(id)) {
|
|
228
|
-
if (callback)
|
|
247
|
+
if (callback) {
|
|
229
248
|
callback(null, { id });
|
|
249
|
+
}
|
|
230
250
|
}
|
|
231
251
|
else {
|
|
232
252
|
ret.setObject(id, obj, callback);
|
|
@@ -238,117 +258,134 @@ function createAdapterMock(db, options = {}) {
|
|
|
238
258
|
target._id = id;
|
|
239
259
|
db.publishObject(target);
|
|
240
260
|
const callback = getCallback(...args);
|
|
241
|
-
if (callback)
|
|
261
|
+
if (callback) {
|
|
242
262
|
callback(null, { id: target._id, value: target }, id);
|
|
263
|
+
}
|
|
243
264
|
}),
|
|
244
265
|
findForeignObject: (0, sinon_1.stub)(),
|
|
245
266
|
delForeignObject: ((id, ...args) => {
|
|
246
267
|
db.deleteObject(id);
|
|
247
268
|
const callback = getCallback(...args);
|
|
248
|
-
if (callback)
|
|
269
|
+
if (callback) {
|
|
249
270
|
callback(undefined);
|
|
271
|
+
}
|
|
250
272
|
}),
|
|
251
273
|
setState: ((id, state, ...args) => {
|
|
252
274
|
const callback = getCallback(...args);
|
|
253
|
-
if (!id.startsWith(ret.namespace))
|
|
254
|
-
id = ret.namespace
|
|
275
|
+
if (!id.startsWith(ret.namespace)) {
|
|
276
|
+
id = `${ret.namespace}.${id}`;
|
|
277
|
+
}
|
|
255
278
|
let ack;
|
|
256
|
-
if (state != null && typeof state ===
|
|
279
|
+
if (state != null && typeof state === 'object') {
|
|
257
280
|
ack = !!state.ack;
|
|
258
281
|
state = state.val;
|
|
259
282
|
}
|
|
260
283
|
else {
|
|
261
|
-
ack = typeof args[0] ===
|
|
284
|
+
ack = typeof args[0] === 'boolean' ? args[0] : false;
|
|
262
285
|
}
|
|
263
286
|
db.publishState(id, { val: state, ack });
|
|
264
|
-
if (callback)
|
|
287
|
+
if (callback) {
|
|
265
288
|
callback(null, id);
|
|
289
|
+
}
|
|
266
290
|
}),
|
|
267
291
|
setStateChanged: ((id, state, ...args) => {
|
|
268
292
|
const callback = getCallback(...args);
|
|
269
293
|
let ack;
|
|
270
|
-
if (state != null && typeof state ===
|
|
294
|
+
if (state != null && typeof state === 'object') {
|
|
271
295
|
ack = !!state.ack;
|
|
272
296
|
state = state.val;
|
|
273
297
|
}
|
|
274
298
|
else {
|
|
275
|
-
ack = typeof args[0] ===
|
|
299
|
+
ack = typeof args[0] === 'boolean' ? args[0] : false;
|
|
300
|
+
}
|
|
301
|
+
if (!id.startsWith(ret.namespace)) {
|
|
302
|
+
id = `${ret.namespace}.${id}`;
|
|
276
303
|
}
|
|
277
|
-
if (!id.startsWith(ret.namespace))
|
|
278
|
-
id = ret.namespace + "." + id;
|
|
279
304
|
if (!db.hasState(id) || db.getState(id).val !== state) {
|
|
280
305
|
db.publishState(id, { val: state, ack });
|
|
281
306
|
}
|
|
282
|
-
if (callback)
|
|
307
|
+
if (callback) {
|
|
283
308
|
callback(null, id);
|
|
309
|
+
}
|
|
284
310
|
}),
|
|
285
311
|
setForeignState: ((id, state, ...args) => {
|
|
286
312
|
const callback = getCallback(...args);
|
|
287
313
|
let ack;
|
|
288
|
-
if (state != null && typeof state ===
|
|
314
|
+
if (state != null && typeof state === 'object') {
|
|
289
315
|
ack = !!state.ack;
|
|
290
316
|
state = state.val;
|
|
291
317
|
}
|
|
292
318
|
else {
|
|
293
|
-
ack = typeof args[0] ===
|
|
319
|
+
ack = typeof args[0] === 'boolean' ? args[0] : false;
|
|
294
320
|
}
|
|
295
321
|
db.publishState(id, { val: state, ack });
|
|
296
|
-
if (callback)
|
|
322
|
+
if (callback) {
|
|
297
323
|
callback(null, id);
|
|
324
|
+
}
|
|
298
325
|
}),
|
|
299
326
|
setForeignStateChanged: ((id, state, ...args) => {
|
|
300
327
|
const callback = getCallback(...args);
|
|
301
328
|
let ack;
|
|
302
|
-
if (state != null && typeof state ===
|
|
329
|
+
if (state != null && typeof state === 'object') {
|
|
303
330
|
ack = !!state.ack;
|
|
304
331
|
state = state.val;
|
|
305
332
|
}
|
|
306
333
|
else {
|
|
307
|
-
ack = typeof args[0] ===
|
|
334
|
+
ack = typeof args[0] === 'boolean' ? args[0] : false;
|
|
308
335
|
}
|
|
309
336
|
if (!db.hasState(id) || db.getState(id).val !== state) {
|
|
310
337
|
db.publishState(id, { val: state, ack });
|
|
311
338
|
}
|
|
312
|
-
if (callback)
|
|
339
|
+
if (callback) {
|
|
313
340
|
callback(null, id);
|
|
341
|
+
}
|
|
314
342
|
}),
|
|
315
343
|
getState: ((id, ...args) => {
|
|
316
|
-
if (!id.startsWith(ret.namespace))
|
|
317
|
-
id = ret.namespace
|
|
344
|
+
if (!id.startsWith(ret.namespace)) {
|
|
345
|
+
id = `${ret.namespace}.${id}`;
|
|
346
|
+
}
|
|
318
347
|
const callback = getCallback(...args);
|
|
319
|
-
if (callback)
|
|
348
|
+
if (callback) {
|
|
320
349
|
callback(null, db.getState(id));
|
|
350
|
+
}
|
|
321
351
|
}),
|
|
322
352
|
getForeignState: ((id, ...args) => {
|
|
323
353
|
const callback = getCallback(...args);
|
|
324
|
-
if (callback)
|
|
354
|
+
if (callback) {
|
|
325
355
|
callback(null, db.getState(id));
|
|
356
|
+
}
|
|
326
357
|
}),
|
|
327
358
|
getStates: ((pattern, ...args) => {
|
|
328
|
-
if (!pattern.startsWith(ret.namespace))
|
|
329
|
-
pattern = ret.namespace
|
|
359
|
+
if (!pattern.startsWith(ret.namespace)) {
|
|
360
|
+
pattern = `${ret.namespace}.${pattern}`;
|
|
361
|
+
}
|
|
330
362
|
const callback = getCallback(...args);
|
|
331
|
-
if (callback)
|
|
363
|
+
if (callback) {
|
|
332
364
|
callback(null, db.getStates(pattern));
|
|
365
|
+
}
|
|
333
366
|
}),
|
|
334
367
|
getForeignStates: ((pattern, ...args) => {
|
|
335
368
|
const callback = getCallback(...args);
|
|
336
|
-
if (callback)
|
|
369
|
+
if (callback) {
|
|
337
370
|
callback(null, db.getStates(pattern));
|
|
371
|
+
}
|
|
338
372
|
}),
|
|
339
373
|
delState: ((id, ...args) => {
|
|
340
|
-
if (!id.startsWith(ret.namespace))
|
|
341
|
-
id = ret.namespace
|
|
374
|
+
if (!id.startsWith(ret.namespace)) {
|
|
375
|
+
id = `${ret.namespace}.${id}`;
|
|
376
|
+
}
|
|
342
377
|
db.deleteState(id);
|
|
343
378
|
const callback = getCallback(...args);
|
|
344
|
-
if (callback)
|
|
379
|
+
if (callback) {
|
|
345
380
|
callback(undefined);
|
|
381
|
+
}
|
|
346
382
|
}),
|
|
347
383
|
delForeignState: ((id, ...args) => {
|
|
348
384
|
db.deleteState(id);
|
|
349
385
|
const callback = getCallback(...args);
|
|
350
|
-
if (callback)
|
|
386
|
+
if (callback) {
|
|
351
387
|
callback(undefined);
|
|
388
|
+
}
|
|
352
389
|
}),
|
|
353
390
|
getHistory: asyncEnabledStub,
|
|
354
391
|
setBinaryState: asyncEnabledStub,
|
|
@@ -388,16 +425,16 @@ function createAdapterMock(db, options = {}) {
|
|
|
388
425
|
formatValue: (0, sinon_1.stub)(),
|
|
389
426
|
formatDate: (0, sinon_1.stub)(),
|
|
390
427
|
terminate: ((reason, exitCode) => {
|
|
391
|
-
if (typeof reason ===
|
|
428
|
+
if (typeof reason === 'number') {
|
|
392
429
|
// Only the exit code was passed
|
|
393
430
|
exitCode = reason;
|
|
394
431
|
reason = undefined;
|
|
395
432
|
}
|
|
396
|
-
const errorMessage = `Adapter.terminate was called${typeof exitCode ===
|
|
433
|
+
const errorMessage = `Adapter.terminate was called${typeof exitCode === 'number' ? ` (exit code ${exitCode})` : ''}: ${reason ? reason : 'Without reason'}`;
|
|
397
434
|
// Terminates execution by
|
|
398
435
|
const err = new Error(errorMessage);
|
|
399
436
|
// @ts-expect-error I'm too lazy to add terminateReason to the error type
|
|
400
|
-
err.terminateReason = reason ||
|
|
437
|
+
err.terminateReason = reason || 'no reason given!';
|
|
401
438
|
throw err;
|
|
402
439
|
}),
|
|
403
440
|
supportsFeature: (0, sinon_1.stub)(),
|
|
@@ -407,19 +444,19 @@ function createAdapterMock(db, options = {}) {
|
|
|
407
444
|
on: ((event, handler) => {
|
|
408
445
|
// Remember the event handlers so we can call them on demand
|
|
409
446
|
switch (event) {
|
|
410
|
-
case
|
|
447
|
+
case 'ready':
|
|
411
448
|
ret.readyHandler = handler;
|
|
412
449
|
break;
|
|
413
|
-
case
|
|
450
|
+
case 'message':
|
|
414
451
|
ret.messageHandler = handler;
|
|
415
452
|
break;
|
|
416
|
-
case
|
|
453
|
+
case 'objectChange':
|
|
417
454
|
ret.objectChangeHandler = handler;
|
|
418
455
|
break;
|
|
419
|
-
case
|
|
456
|
+
case 'stateChange':
|
|
420
457
|
ret.stateChangeHandler = handler;
|
|
421
458
|
break;
|
|
422
|
-
case
|
|
459
|
+
case 'unload':
|
|
423
460
|
ret.unloadHandler = handler;
|
|
424
461
|
break;
|
|
425
462
|
}
|
|
@@ -428,38 +465,38 @@ function createAdapterMock(db, options = {}) {
|
|
|
428
465
|
removeListener: ((event, _listener) => {
|
|
429
466
|
// TODO This is not entirely correct
|
|
430
467
|
switch (event) {
|
|
431
|
-
case
|
|
468
|
+
case 'ready':
|
|
432
469
|
ret.readyHandler = undefined;
|
|
433
470
|
break;
|
|
434
|
-
case
|
|
471
|
+
case 'message':
|
|
435
472
|
ret.messageHandler = undefined;
|
|
436
473
|
break;
|
|
437
|
-
case
|
|
474
|
+
case 'objectChange':
|
|
438
475
|
ret.objectChangeHandler = undefined;
|
|
439
476
|
break;
|
|
440
|
-
case
|
|
477
|
+
case 'stateChange':
|
|
441
478
|
ret.stateChangeHandler = undefined;
|
|
442
479
|
break;
|
|
443
|
-
case
|
|
480
|
+
case 'unload':
|
|
444
481
|
ret.unloadHandler = undefined;
|
|
445
482
|
break;
|
|
446
483
|
}
|
|
447
484
|
return ret;
|
|
448
485
|
}),
|
|
449
486
|
removeAllListeners: ((event) => {
|
|
450
|
-
if (!event || event ===
|
|
487
|
+
if (!event || event === 'ready') {
|
|
451
488
|
ret.readyHandler = undefined;
|
|
452
489
|
}
|
|
453
|
-
if (!event || event ===
|
|
490
|
+
if (!event || event === 'message') {
|
|
454
491
|
ret.messageHandler = undefined;
|
|
455
492
|
}
|
|
456
|
-
if (!event || event ===
|
|
493
|
+
if (!event || event === 'objectChange') {
|
|
457
494
|
ret.objectChangeHandler = undefined;
|
|
458
495
|
}
|
|
459
|
-
if (!event || event ===
|
|
496
|
+
if (!event || event === 'stateChange') {
|
|
460
497
|
ret.stateChangeHandler = undefined;
|
|
461
498
|
}
|
|
462
|
-
if (!event || event ===
|
|
499
|
+
if (!event || event === 'unload') {
|
|
463
500
|
ret.unloadHandler = undefined;
|
|
464
501
|
}
|
|
465
502
|
return ret;
|
|
@@ -480,10 +517,7 @@ function createAdapterMock(db, options = {}) {
|
|
|
480
517
|
ret.resetMockBehavior();
|
|
481
518
|
},
|
|
482
519
|
});
|
|
483
|
-
(0, tools_1.stubAndPromisifyImplementedMethods)(ret, implementedMethods, [
|
|
484
|
-
"getObjectView",
|
|
485
|
-
"getObjectList",
|
|
486
|
-
]);
|
|
520
|
+
(0, tools_1.stubAndPromisifyImplementedMethods)(ret, implementedMethods, ['getObjectView', 'getObjectList']);
|
|
487
521
|
// Access the options object directly, so we can react to later changes
|
|
488
522
|
Object.defineProperties(ret, {
|
|
489
523
|
readyHandler: {
|
|
@@ -1,9 +1,5 @@
|
|
|
1
|
-
import { MockAdapter } from
|
|
2
|
-
import type { MockDatabase } from
|
|
3
|
-
interface MockAdapterConstructor {
|
|
4
|
-
new (nameOrOptions: string | ioBroker.AdapterOptions): MockAdapter;
|
|
5
|
-
(nameOrOptions: string | ioBroker.AdapterOptions): MockAdapter;
|
|
6
|
-
}
|
|
1
|
+
import { type MockAdapter } from './mockAdapter';
|
|
2
|
+
import type { MockDatabase } from './mockDatabase';
|
|
7
3
|
export interface MockAdapterCoreOptions {
|
|
8
4
|
onAdapterCreated?: (adapter: MockAdapter) => void;
|
|
9
5
|
adapterDir?: string;
|
|
@@ -11,9 +7,8 @@ export interface MockAdapterCoreOptions {
|
|
|
11
7
|
export declare function mockAdapterCore(database: MockDatabase, options?: MockAdapterCoreOptions): {
|
|
12
8
|
controllerDir: string;
|
|
13
9
|
getConfig: () => Record<string, any>;
|
|
14
|
-
Adapter:
|
|
15
|
-
adapter:
|
|
10
|
+
Adapter: (this: MockAdapter | void, nameOrOptions: string | ioBroker.AdapterOptions) => MockAdapter;
|
|
11
|
+
adapter: (this: MockAdapter | void, nameOrOptions: string | ioBroker.AdapterOptions) => MockAdapter;
|
|
16
12
|
getAbsoluteDefaultDataDir: () => string;
|
|
17
13
|
getAbsoluteInstanceDataDir: (adapterObject: MockAdapter) => string;
|
|
18
14
|
};
|
|
19
|
-
export {};
|