-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathmuse.spec.ts
441 lines (356 loc) · 18.6 KB
/
muse.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
import { TextDecoder, TextEncoder } from 'text-encoding';
import { DeviceMock, WebBluetoothMock } from 'web-bluetooth-mock';
import { EEGReading, PPGReading } from './../dist/lib/muse-interfaces.d';
import { MuseClient } from './muse';
declare var global;
let museDevice: DeviceMock;
function charCodes(s) {
return s.split('').map((c) => c.charCodeAt(0));
}
describe('MuseClient', () => {
beforeEach(() => {
museDevice = new DeviceMock('Muse-Test', [0xfe8d]);
global.navigator = global.navigator || {};
global.navigator.bluetooth = new WebBluetoothMock([museDevice]);
Object.assign(global, { TextDecoder, TextEncoder });
});
describe('connect', () => {
it('should connect to EEG headset', async () => {
const client = new MuseClient();
await client.connect();
});
it('should call startNotifications() on the EEG electrode characteritics', async () => {
const service = museDevice.getServiceMock(0xfe8d);
const eeg1Char = service.getCharacteristicMock('273e0003-4c4d-454d-96be-f03bac821358');
eeg1Char.startNotifications = jest.fn();
const client = new MuseClient();
await client.connect();
expect(eeg1Char.startNotifications).toHaveBeenCalled();
});
it('should call startNotifications() on the PPG channel characteritics', async () => {
const service = museDevice.getServiceMock(0xfe8d);
const ppg1Char = service.getCharacteristicMock('273e000f-4c4d-454d-96be-f03bac821358');
ppg1Char.startNotifications = jest.fn();
const client = new MuseClient();
client.enablePpg = true;
await client.connect();
expect(ppg1Char.startNotifications).toHaveBeenCalled();
});
it('should not call startNotifications() on the Aux EEG electrode by default', async () => {
const service = museDevice.getServiceMock(0xfe8d);
const eegAuxChar = service.getCharacteristicMock('273e0007-4c4d-454d-96be-f03bac821358');
eegAuxChar.startNotifications = jest.fn();
const client = new MuseClient();
await client.connect();
expect(eegAuxChar.startNotifications).not.toHaveBeenCalled();
});
it('should call startNotifications() on the Aux EEG electrode when enableAux is set to true', async () => {
const service = museDevice.getServiceMock(0xfe8d);
const eegAuxChar = service.getCharacteristicMock('273e0007-4c4d-454d-96be-f03bac821358');
eegAuxChar.startNotifications = jest.fn();
const client = new MuseClient();
client.enableAux = true;
await client.connect();
expect(eegAuxChar.startNotifications).toHaveBeenCalled();
});
});
describe('start', async () => {
it('should send `h`, `s`, `p21` and `d` commands to the EEG headset', async () => {
const client = new MuseClient();
const controlCharacteristic = museDevice
.getServiceMock(0xfe8d)
.getCharacteristicMock('273e0001-4c4d-454d-96be-f03bac821358');
controlCharacteristic.writeValue = jest.fn();
await client.connect();
await client.start();
expect(controlCharacteristic.writeValue).toHaveBeenCalledWith(new Uint8Array([2, ...charCodes('h'), 10]));
expect(controlCharacteristic.writeValue).toHaveBeenCalledWith(new Uint8Array([2, ...charCodes('s'), 10]));
expect(controlCharacteristic.writeValue).toHaveBeenCalledWith(new Uint8Array([4, ...charCodes('p21'), 10]));
expect(controlCharacteristic.writeValue).toHaveBeenCalledWith(new Uint8Array([2, ...charCodes('d'), 10]));
});
it('choose preset number 20 instead of 21 if aux is enabled', async () => {
const client = new MuseClient();
const controlCharacteristic = museDevice
.getServiceMock(0xfe8d)
.getCharacteristicMock('273e0001-4c4d-454d-96be-f03bac821358');
controlCharacteristic.writeValue = jest.fn();
client.enableAux = true;
await client.connect();
await client.start();
expect(controlCharacteristic.writeValue).toHaveBeenCalledWith(new Uint8Array([4, ...charCodes('p20'), 10]));
expect(controlCharacteristic.writeValue).not.toHaveBeenCalledWith(
new Uint8Array([4, ...charCodes('p21'), 10]),
);
});
it('choose preset number 50 instead of 20/21 if ppg is enabled', async () => {
const client = new MuseClient();
const controlCharacteristic = museDevice
.getServiceMock(0xfe8d)
.getCharacteristicMock('273e0001-4c4d-454d-96be-f03bac821358');
controlCharacteristic.writeValue = jest.fn();
client.enableAux = true;
client.enablePpg = true;
await client.connect();
await client.start();
expect(controlCharacteristic.writeValue).toHaveBeenCalledWith(new Uint8Array([4, ...charCodes('p50'), 10]));
expect(controlCharacteristic.writeValue).not.toHaveBeenCalledWith(
new Uint8Array([4, ...charCodes('p21'), 10]),
);
});
});
describe('eegReadings', () => {
it('should emit a value for `eegReadings` observable whenever new EEG data is received', async () => {
const service = museDevice.getServiceMock(0xfe8d);
const eeg3Char = service.getCharacteristicMock('273e0006-4c4d-454d-96be-f03bac821358');
const client = new MuseClient();
await client.connect();
let lastReading: EEGReading;
client.eegReadings.subscribe((reading) => {
lastReading = reading;
});
eeg3Char.value = new DataView(
new Uint8Array([0, 1, 40, 3, 128, 40, 3, 128, 40, 3, 128, 40, 3, 128, 40, 3, 128, 40, 3, 128]).buffer,
);
const beforeDispatchTime = new Date().getTime();
eeg3Char.dispatchEvent(new CustomEvent('characteristicvaluechanged'));
const afterDispatchTime = new Date().getTime();
expect(lastReading).toEqual({
electrode: 3,
index: 1,
samples: [
-687.5,
-562.5,
-687.5,
-562.5,
-687.5,
-562.5,
-687.5,
-562.5,
-687.5,
-562.5,
-687.5,
-562.5,
],
timestamp: expect.any(Number),
});
// Timestamp should be about (1000/256.0*12) milliseconds behind the event dispatch time
expect(lastReading.timestamp).toBeGreaterThanOrEqual(beforeDispatchTime - (1000 / 256.0) * 12);
expect(lastReading.timestamp).toBeLessThanOrEqual(afterDispatchTime - (1000 / 256.0) * 12);
});
it('should report the same timestamp for eeg events with the same sequence', async () => {
const service = museDevice.getServiceMock(0xfe8d);
const eeg1Char = service.getCharacteristicMock('273e0004-4c4d-454d-96be-f03bac821358');
const eeg3Char = service.getCharacteristicMock('273e0006-4c4d-454d-96be-f03bac821358');
const client = new MuseClient();
await client.connect();
const readings: EEGReading[] = [];
client.eegReadings.subscribe((reading) => {
readings.push(reading);
});
eeg1Char.value = new DataView(new Uint8Array([0, 15]).buffer);
eeg1Char.dispatchEvent(new CustomEvent('characteristicvaluechanged'));
eeg3Char.value = new DataView(new Uint8Array([0, 15]).buffer);
eeg3Char.dispatchEvent(new CustomEvent('characteristicvaluechanged'));
expect(readings.length).toBe(2);
expect(readings[0].electrode).toBe(1);
expect(readings[1].electrode).toBe(3);
expect(readings[0].timestamp).toEqual(readings[1].timestamp);
});
it('should bump the timestamp for subsequent EEG events', async () => {
const service = museDevice.getServiceMock(0xfe8d);
const eeg1Char = service.getCharacteristicMock('273e0004-4c4d-454d-96be-f03bac821358');
const client = new MuseClient();
await client.connect();
const readings: EEGReading[] = [];
client.eegReadings.subscribe((reading) => {
readings.push(reading);
});
eeg1Char.value = new DataView(new Uint8Array([0, 15]).buffer);
eeg1Char.dispatchEvent(new CustomEvent('characteristicvaluechanged'));
eeg1Char.value = new DataView(new Uint8Array([0, 16]).buffer);
eeg1Char.dispatchEvent(new CustomEvent('characteristicvaluechanged'));
expect(readings[1].timestamp - readings[0].timestamp).toEqual(1000 / (256.0 / 12.0));
});
it('should correctly handle out-of-order EEG events', async () => {
const service = museDevice.getServiceMock(0xfe8d);
const eeg1Char = service.getCharacteristicMock('273e0004-4c4d-454d-96be-f03bac821358');
const client = new MuseClient();
await client.connect();
const readings: EEGReading[] = [];
client.eegReadings.subscribe((reading) => {
readings.push(reading);
});
eeg1Char.value = new DataView(new Uint8Array([0, 20]).buffer);
eeg1Char.dispatchEvent(new CustomEvent('characteristicvaluechanged'));
eeg1Char.value = new DataView(new Uint8Array([0, 16]).buffer);
eeg1Char.dispatchEvent(new CustomEvent('characteristicvaluechanged'));
expect(readings[1].timestamp - readings[0].timestamp).toEqual((-4 * 1000) / (256.0 / 12.0));
});
it('should handle timestamp wraparound', async () => {
const service = museDevice.getServiceMock(0xfe8d);
const eeg1Char = service.getCharacteristicMock('273e0004-4c4d-454d-96be-f03bac821358');
const client = new MuseClient();
await client.connect();
const readings: EEGReading[] = [];
client.eegReadings.subscribe((reading) => {
readings.push(reading);
});
eeg1Char.value = new DataView(new Uint8Array([0xff, 0xff]).buffer);
eeg1Char.dispatchEvent(new CustomEvent('characteristicvaluechanged'));
eeg1Char.value = new DataView(new Uint8Array([0, 0]).buffer);
eeg1Char.dispatchEvent(new CustomEvent('characteristicvaluechanged'));
expect(readings[1].timestamp - readings[0].timestamp).toEqual(1000 / (256.0 / 12.0));
});
});
describe('ppgReadings', () => {
it('should report the same timestamp for ppg events with the same sequence', async () => {
const service = museDevice.getServiceMock(0xfe8d);
const ppg0Char = service.getCharacteristicMock('273e000f-4c4d-454d-96be-f03bac821358');
const ppg1Char = service.getCharacteristicMock('273e0010-4c4d-454d-96be-f03bac821358');
const client = new MuseClient();
client.enablePpg = true;
await client.connect();
const readings: PPGReading[] = [];
client.ppgReadings.subscribe((reading) => {
readings.push(reading);
});
ppg0Char.value = new DataView(new Uint8Array([0, 15]).buffer);
ppg0Char.dispatchEvent(new CustomEvent('characteristicvaluechanged'));
ppg1Char.value = new DataView(new Uint8Array([0, 15]).buffer);
ppg1Char.dispatchEvent(new CustomEvent('characteristicvaluechanged'));
expect(readings.length).toBe(2);
expect(readings[0].ppgChannel).toBe(0);
expect(readings[1].ppgChannel).toBe(1);
expect(readings[0].timestamp).toEqual(readings[1].timestamp);
});
it('should bump the timestamp for subsequent PPG events', async () => {
const service = museDevice.getServiceMock(0xfe8d);
const ppg0Char = service.getCharacteristicMock('273e000f-4c4d-454d-96be-f03bac821358');
const client = new MuseClient();
client.enableAux = true;
client.enablePpg = true;
await client.connect();
const readings: PPGReading[] = [];
client.ppgReadings.subscribe((reading) => {
readings.push(reading);
});
ppg0Char.value = new DataView(new Uint8Array([0, 15]).buffer);
ppg0Char.dispatchEvent(new CustomEvent('characteristicvaluechanged'));
ppg0Char.value = new DataView(new Uint8Array([0, 16]).buffer);
ppg0Char.dispatchEvent(new CustomEvent('characteristicvaluechanged'));
expect(readings[1].timestamp - readings[0].timestamp).toEqual(1000 / (64.0 / 6.0));
});
it('should correctly handle out-of-order PPG events', async () => {
const service = museDevice.getServiceMock(0xfe8d);
const ppg1Char = service.getCharacteristicMock('273e0010-4c4d-454d-96be-f03bac821358');
const client = new MuseClient();
client.enableAux = true;
client.enablePpg = true;
await client.connect();
const readings: PPGReading[] = [];
client.ppgReadings.subscribe((reading) => {
readings.push(reading);
});
ppg1Char.value = new DataView(new Uint8Array([0, 20]).buffer);
ppg1Char.dispatchEvent(new CustomEvent('characteristicvaluechanged'));
ppg1Char.value = new DataView(new Uint8Array([0, 16]).buffer);
ppg1Char.dispatchEvent(new CustomEvent('characteristicvaluechanged'));
expect(readings[1].timestamp - readings[0].timestamp).toEqual((-4 * 1000) / (64.0 / 6.0));
});
});
describe('deviceInfo', () => {
it('should return information about the headset', async () => {
const service = museDevice.getServiceMock(0xfe8d);
const controlCharacteristic = service.getCharacteristicMock('273e0001-4c4d-454d-96be-f03bac821358');
jest.spyOn(controlCharacteristic, 'writeValue');
const client = new MuseClient();
await client.connect();
const deviceInfoPromise = client.deviceInfo();
expect(controlCharacteristic.writeValue).toHaveBeenCalledWith(new Uint8Array([3, ...charCodes('v1'), 10]));
const deviceResponse = [
[16, 123, 34, 97, 112, 34, 58, 34, 104, 101, 97, 100, 115, 101, 116, 34, 44, 50, 51, 49],
[12, 34, 115, 112, 34, 58, 34, 82, 101, 118, 69, 34, 44, 101, 116, 34, 44, 50, 51, 49],
[16, 34, 116, 112, 34, 58, 34, 99, 111, 110, 115, 117, 109, 101, 114, 34, 44, 50, 51, 49],
[11, 34, 104, 119, 34, 58, 34, 51, 46, 49, 34, 44, 109, 101, 114, 34, 44, 50, 51, 49],
[8, 34, 98, 110, 34, 58, 50, 55, 44, 49, 34, 44, 109, 101, 114, 34, 44, 50, 51, 49],
[14, 34, 102, 119, 34, 58, 34, 49, 46, 50, 46, 49, 51, 34, 44, 34, 44, 50, 51, 49],
[13, 34, 98, 108, 34, 58, 34, 49, 46, 50, 46, 51, 34, 44, 44, 34, 44, 50, 51, 49],
[7, 34, 112, 118, 34, 58, 49, 44, 46, 50, 46, 51, 34, 44, 44, 34, 44, 50, 51, 49],
[7, 34, 114, 99, 34, 58, 48, 125, 46, 50, 46, 51, 34, 44, 44, 34, 44, 50, 51, 49],
];
deviceResponse.forEach((data) => {
controlCharacteristic.value = new DataView(new Uint8Array(data).buffer);
controlCharacteristic.dispatchEvent(new CustomEvent('characteristicvaluechanged'));
});
const deviceInfo = await deviceInfoPromise;
expect(deviceInfo).toEqual({
ap: 'headset',
bl: '1.2.3',
bn: 27,
fw: '1.2.13',
hw: '3.1',
pv: 1,
rc: 0,
sp: 'RevE',
tp: 'consumer',
});
});
});
describe('disconnect', () => {
it('should disconnect from gatt', async () => {
const client = new MuseClient();
await client.connect();
jest.spyOn(museDevice.gatt, 'disconnect');
client.disconnect();
expect(museDevice.gatt.disconnect).toHaveBeenCalled();
});
it('should emit a disconnect event', async () => {
const client = new MuseClient();
let lastStatus = null;
client.connectionStatus.subscribe((value) => {
lastStatus = value;
});
await client.connect();
expect(lastStatus).toBe(true);
client.disconnect();
expect(lastStatus).toBe(false);
});
it('should silently return if connect() was not valled', async () => {
const client = new MuseClient();
client.disconnect();
});
});
describe('eventMarkers', () => {
it('should emit a marker whenever injectMarker is called', async () => {
const client = new MuseClient();
await client.connect();
const markers = [];
client.eventMarkers.subscribe((eventMarker) => {
markers.push(eventMarker);
});
await client.injectMarker('face', 1532808289990);
await client.injectMarker('house', 1532808281390);
await client.injectMarker('face', 1532808282390);
await client.injectMarker('house', 1532808285390);
expect(markers.length).toBe(4);
expect(markers[markers.length - 1]).toEqual({ value: 'house', timestamp: 1532808285390 });
});
it('should be able to timestamp on its own', async () => {
const client = new MuseClient();
await client.connect();
const markers = [];
client.eventMarkers.subscribe((eventMarker) => {
markers.push(eventMarker);
});
const startTime = new Date().getTime();
await client.injectMarker('house');
await client.injectMarker('face');
await client.injectMarker('house');
await client.injectMarker('face');
expect(markers.length).toBe(4);
const lastMarker = markers[markers.length - 1];
expect(lastMarker.timestamp).toBeGreaterThanOrEqual(startTime);
expect(lastMarker.timestamp).toBeLessThanOrEqual(new Date().getTime());
});
});
});