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