-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bluetooth-classic.service.spec.ts
369 lines (317 loc) · 12.1 KB
/
bluetooth-classic.service.spec.ts
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import { Test, TestingModule } from '@nestjs/testing';
import { BluetoothClassicService } from './bluetooth-classic.service';
import { ConfigModule } from '../../config/config.module';
import { EntitiesModule } from '../../entities/entities.module';
import { ClusterModule } from '../../cluster/cluster.module';
import { EntitiesService } from '../../entities/entities.service';
import { ClusterService } from '../../cluster/cluster.service';
import { ScheduleModule } from '@nestjs/schedule';
import * as util from 'util';
import {
NEW_RSSI_CHANNEL,
REQUEST_RSSI_CHANNEL
} from './bluetooth-classic.const';
import { NewRssiEvent } from './new-rssi.event';
import { RoomPresenceDistanceSensor } from '../room-presence/room-presence-distance.sensor';
import KalmanFilter from 'kalmanjs';
jest.mock('child_process');
jest.mock('util');
jest.mock('../room-presence/room-presence-distance.sensor');
jest.mock('kalmanjs', () => {
return jest.fn().mockImplementation(() => {
return {
filter: z => z
};
});
});
describe('BluetoothClassicService', () => {
let service: BluetoothClassicService;
const entitiesService = {
add: jest.fn(),
get: jest.fn(),
has: jest.fn()
};
const clusterService = {
on: jest.fn(),
nodes: jest.fn(),
send: jest.fn(),
publish: jest.fn(),
subscribe: jest.fn(),
isLeader: jest.fn()
};
const loggerService = {
log: jest.fn(),
error: jest.fn(),
warn: jest.fn()
};
beforeEach(async () => {
jest.clearAllMocks();
const module: TestingModule = await Test.createTestingModule({
imports: [
ConfigModule,
EntitiesModule,
ClusterModule,
ScheduleModule.forRoot()
],
providers: [BluetoothClassicService]
})
.overrideProvider(EntitiesService)
.useValue(entitiesService)
.overrideProvider(ClusterService)
.useValue(clusterService)
.compile();
module.useLogger(loggerService);
service = module.get<BluetoothClassicService>(BluetoothClassicService);
});
it('should log an error if hcitool is not installed', async () => {
jest.spyOn(util, 'promisify').mockImplementation(() => {
return jest.fn().mockRejectedValue({ stderr: 'hcitool not found' });
});
await service.onModuleInit();
expect(loggerService.error).toHaveBeenCalledWith(
expect.stringContaining('could not be found'),
expect.anything(),
BluetoothClassicService.name
);
});
it('should not log an error if hcitool is found', async () => {
jest.spyOn(util, 'promisify').mockImplementation(() => {
return jest.fn().mockResolvedValue({ stdout: 'hcitool help' });
});
await service.onModuleInit();
expect(loggerService.error).not.toHaveBeenCalled();
});
it('should setup the cluster bindings on bootstrap', () => {
service.onApplicationBootstrap();
expect(clusterService.on).toHaveBeenCalledWith(
REQUEST_RSSI_CHANNEL,
expect.anything()
);
expect(clusterService.on).toHaveBeenCalledWith(
NEW_RSSI_CHANNEL,
expect.anything()
);
expect(clusterService.subscribe).toHaveBeenCalledWith(NEW_RSSI_CHANNEL);
});
it('should return measured RSSI value from command output', () => {
jest.spyOn(util, 'promisify').mockImplementation(() => {
return jest.fn().mockResolvedValue({ stdout: 'RSSI return value: -4' });
});
const address = '77:50:fb:4d:ab:70';
expect(service.inquireRssi(address)).resolves.toBe(-4);
});
it('should return undefined if no RSSI could be determined', () => {
jest.spyOn(util, 'promisify').mockImplementation(() => {
return jest.fn().mockResolvedValue({
stdout: "Can't create connection: Input/output error",
stderr: 'Not connected.'
});
});
expect(service.inquireRssi('08:05:90:ed:3b:60')).resolves.toBeUndefined();
});
it('should return the Bluetooth device name if found', async () => {
jest.spyOn(util, 'promisify').mockImplementation(() => {
return jest.fn().mockResolvedValue({ stdout: 'Test iPhone' });
});
expect(await service.inquireDeviceName('bb:3e:db:b7:8a:60')).toBe(
'Test iPhone'
);
});
it('should return undefined for name if not found', async () => {
jest.spyOn(util, 'promisify').mockImplementation(() => {
return jest.fn().mockResolvedValue({ stdout: '' });
});
expect(
await service.inquireDeviceName('bb:3e:db:b7:8a:60')
).toBeUndefined();
});
it('should publish the RSSI if found', async () => {
jest.spyOn(service, 'inquireRssi').mockResolvedValue(0);
const handleRssiMock = jest
.spyOn(service, 'handleNewRssi')
.mockImplementation(() => undefined);
const address = '77:50:fb:4d:ab:70';
const expectedEvent = new NewRssiEvent('test-instance', address, 0);
await service.handleRssiRequest(address);
expect(clusterService.publish).toHaveBeenCalledWith(
NEW_RSSI_CHANNEL,
expectedEvent
);
expect(handleRssiMock).toHaveBeenCalledWith(expectedEvent);
});
it('should not publish an RSSI value if none was found', async () => {
jest.spyOn(service, 'inquireRssi').mockResolvedValue(undefined);
const handleRssiMock = jest
.spyOn(service, 'handleNewRssi')
.mockImplementation(() => undefined);
await service.handleRssiRequest('77:50:fb:4d:ab:70');
expect(clusterService.publish).not.toHaveBeenCalled();
expect(handleRssiMock).not.toHaveBeenCalled();
});
it('should register a new sensor for a previously unknown device', async () => {
entitiesService.has.mockReturnValue(false);
entitiesService.add.mockImplementation(entity => entity);
clusterService.nodes.mockReturnValue({
abcd: { channels: [NEW_RSSI_CHANNEL] }
});
jest.spyOn(service, 'inquireDeviceName').mockResolvedValue('Test iPhone');
jest.useFakeTimers();
const event = new NewRssiEvent('test-instance', '10:36:cf:ca:9a:18', -10);
await service.handleNewRssi(event);
expect(entitiesService.add).toHaveBeenCalledWith(
expect.any(RoomPresenceDistanceSensor),
expect.any(Array)
);
expect(setInterval).toHaveBeenCalledWith(expect.any(Function), 10000);
const sensorInstance = (RoomPresenceDistanceSensor as jest.Mock).mock
.instances[0];
expect(sensorInstance.handleNewDistance).toHaveBeenCalledWith(
'test-instance',
10
);
expect(sensorInstance.timeout).toBe(35);
});
it('should not distribute inquiries if not the leader', () => {
clusterService.isLeader.mockReturnValue(false);
const inquireSpy = jest.spyOn(service, 'inquireRssi');
service.distributeInquiries();
expect(clusterService.send).not.toHaveBeenCalled();
expect(inquireSpy).not.toHaveBeenCalled();
});
it('should rotate inquiries correctly when there are more addresses than nodes', () => {
clusterService.nodes.mockReturnValue({
abcd: { channels: [NEW_RSSI_CHANNEL] }
});
clusterService.isLeader.mockReturnValue(true);
const rssiRequestSpy = jest
.spyOn(service, 'handleRssiRequest')
.mockImplementation(() => undefined);
service.distributeInquiries();
expect(rssiRequestSpy).toHaveBeenLastCalledWith('8d:ad:e3:e2:7a:01');
service.distributeInquiries();
expect(rssiRequestSpy).toHaveBeenLastCalledWith('f7:6c:e3:10:55:b5');
service.distributeInquiries();
expect(rssiRequestSpy).toHaveBeenLastCalledWith('8d:ad:e3:e2:7a:01');
});
it('should rotate inquiries correctly when there are exactly as many addresses as nodes', () => {
clusterService.nodes.mockReturnValue({
abcd: { id: 'abcd', channels: [NEW_RSSI_CHANNEL] },
def: { id: 'def', channels: [NEW_RSSI_CHANNEL], last: new Date() }
});
clusterService.isLeader.mockReturnValue(true);
const handleRssiRequest = jest
.spyOn(service, 'handleRssiRequest')
.mockImplementation(() => undefined);
service.distributeInquiries();
expect(handleRssiRequest).toHaveBeenLastCalledWith('8d:ad:e3:e2:7a:01');
expect(clusterService.send).toHaveBeenLastCalledWith(
REQUEST_RSSI_CHANNEL,
'f7:6c:e3:10:55:b5',
'def'
);
service.distributeInquiries();
expect(handleRssiRequest).toHaveBeenLastCalledWith('f7:6c:e3:10:55:b5');
expect(clusterService.send).toHaveBeenLastCalledWith(
REQUEST_RSSI_CHANNEL,
'8d:ad:e3:e2:7a:01',
'def'
);
service.distributeInquiries();
expect(handleRssiRequest).toHaveBeenLastCalledWith('8d:ad:e3:e2:7a:01');
expect(clusterService.send).toHaveBeenLastCalledWith(
REQUEST_RSSI_CHANNEL,
'f7:6c:e3:10:55:b5',
'def'
);
});
it('should rotate inquiries correctly when there are more nodes than addresses', () => {
clusterService.nodes.mockReturnValue({
abcd: { id: 'abcd', channels: [NEW_RSSI_CHANNEL] },
def: { id: 'def', channels: [NEW_RSSI_CHANNEL], last: new Date() },
xyz: { id: 'xyz', channels: [NEW_RSSI_CHANNEL], last: new Date() }
});
clusterService.isLeader.mockReturnValue(true);
const handleRssiRequest = jest
.spyOn(service, 'handleRssiRequest')
.mockImplementation(() => undefined);
service.distributeInquiries();
expect(handleRssiRequest).toHaveBeenLastCalledWith('8d:ad:e3:e2:7a:01');
expect(clusterService.send).toHaveBeenCalledWith(
REQUEST_RSSI_CHANNEL,
'f7:6c:e3:10:55:b5',
'def'
);
expect(clusterService.send).toHaveBeenCalledTimes(1);
handleRssiRequest.mockClear();
clusterService.send.mockClear();
service.distributeInquiries();
expect(handleRssiRequest).not.toHaveBeenCalled();
expect(clusterService.send).toHaveBeenCalledWith(
REQUEST_RSSI_CHANNEL,
'8d:ad:e3:e2:7a:01',
'def'
);
expect(clusterService.send).toHaveBeenCalledWith(
REQUEST_RSSI_CHANNEL,
'f7:6c:e3:10:55:b5',
'xyz'
);
expect(clusterService.send).toHaveBeenCalledTimes(2);
handleRssiRequest.mockClear();
clusterService.send.mockClear();
service.distributeInquiries();
expect(handleRssiRequest).toHaveBeenCalledWith('f7:6c:e3:10:55:b5');
expect(clusterService.send).toHaveBeenCalledWith(
REQUEST_RSSI_CHANNEL,
'8d:ad:e3:e2:7a:01',
'xyz'
);
expect(clusterService.send).toHaveBeenCalledTimes(1);
handleRssiRequest.mockClear();
clusterService.send.mockClear();
service.distributeInquiries();
expect(handleRssiRequest).toHaveBeenLastCalledWith('8d:ad:e3:e2:7a:01');
expect(clusterService.send).toHaveBeenCalledWith(
REQUEST_RSSI_CHANNEL,
'f7:6c:e3:10:55:b5',
'def'
);
expect(clusterService.send).toHaveBeenCalledTimes(1);
});
it('should only account for nodes that have the integration enabled', () => {
clusterService.nodes.mockReturnValue({
abcd: { id: 'abcd', channels: [NEW_RSSI_CHANNEL] },
def: { id: 'def', channels: [NEW_RSSI_CHANNEL], last: new Date() },
xyz: { id: 'xyz', last: new Date() }
});
const nodes = service.getParticipatingNodes();
expect(nodes).toHaveLength(2);
expect(nodes.find(node => node.id === 'abcd')).not.toBeUndefined();
expect(nodes.find(node => node.id === 'def')).not.toBeUndefined();
expect(nodes.find(node => node.id === 'xyz')).toBeUndefined();
});
it('should filter the RSSI of inquired devices before publishing', async () => {
jest.spyOn(service, 'inquireRssi').mockResolvedValue(-3);
const handleRssiMock = jest
.spyOn(service, 'handleNewRssi')
.mockImplementation(() => undefined);
const filterRssiMock = jest
.spyOn(service, 'filterRssi')
.mockReturnValue(-5.2);
const address = 'ab:cd:01:23:00:70';
const expectedEvent = new NewRssiEvent('test-instance', address, -5.2);
await service.handleRssiRequest(address);
expect(filterRssiMock).toHaveBeenCalled();
expect(clusterService.publish).toHaveBeenCalledWith(
NEW_RSSI_CHANNEL,
expectedEvent
);
expect(handleRssiMock).toHaveBeenCalledWith(expectedEvent);
});
it('should reuse Kalman filters for the same address', () => {
service.filterRssi('D6:AB:CD:10:DA:31', -1);
service.filterRssi('D6:AB:CD:10:DA:41', -4);
service.filterRssi('D6:AB:CD:10:DA:31', -2);
expect(KalmanFilter).toHaveBeenCalledTimes(2);
});
});