forked from openbci-archive/OpenBCI_NodeJS
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathopenBCIBoard-test.js
2936 lines (2720 loc) · 122 KB
/
openBCIBoard-test.js
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var sinon = require('sinon'),
chai = require('chai'),
should = chai.should(),
expect = chai.expect,
openBCIBoard = require('../openBCIBoard'),
openBCISample = openBCIBoard.OpenBCISample,
k = openBCISample.k,
chaiAsPromised = require("chai-as-promised"),
sinonChai = require("sinon-chai"),
bufferEqual = require('buffer-equal'),
fs = require('fs'),
math = require('mathjs');
chai.use(chaiAsPromised);
chai.use(sinonChai);
describe('openbci-sdk',function() {
this.timeout(2000);
var ourBoard, masterPortName, realBoard, spy;
before(function(done) {
ourBoard = new openBCIBoard.OpenBCIBoard();
ourBoard.autoFindOpenBCIBoard()
.then(portName => {
ourBoard = null;
realBoard = true;
masterPortName = portName;
done();
})
.catch(err => {
ourBoard = null;
realBoard = false;
masterPortName = k.OBCISimulatorPortName;
done();
})
});
after(done => {
if (ourBoard) {
if(ourBoard["connected"]) {
ourBoard.disconnect()
.then(() => {
done();
})
.catch(err => {
done(err);
})
} else {
done();
}
} else {
done();
}
});
describe('#constructor', function () {
afterEach(() => {
ourBoard = null;
});
it('constructs with require', function() {
var OpenBCIBoard = require('../openBCIBoard').OpenBCIBoard;
ourBoard = new OpenBCIBoard({
verbose:true
});
expect(ourBoard.numberOfChannels()).to.equal(8);
});
it('constructs with the correct default options', () => {
var board = new openBCIBoard.OpenBCIBoard();
expect(board.options.boardType).to.equal(k.OBCIBoardDefault);
expect(board.options.baudRate).to.equal(115200);
expect(board.options.simulate).to.be.false;
expect(board.options.simulatorBoardFailure).to.be.false;
expect(board.options.simulatorDaisyModuleAttached).to.be.false;
expect(board.options.simulatorFirmwareVersion).to.equal(k.OBCIFirmwareV1);
expect(board.options.simulatorHasAccelerometer).to.be.true;
expect(board.options.simulatorInternalClockDrift).to.equal(0);
expect(board.options.simulatorInjectAlpha).to.be.true;
expect(board.options.simulatorInjectLineNoise).to.equal(k.OBCISimulatorLineNoiseHz60);
expect(board.options.simulatorSampleRate).to.equal(k.OBCISampleRate250);
expect(board.options.simulatorSerialPortFailure).to.be.false;
expect(board.options.sntpTimeSync).to.be.false;
expect(board.options.sntpTimeSyncHost).to.equal('pool.ntp.org');
expect(board.options.verbose).to.be.false;
expect(board.sampleRate()).to.equal(250);
expect(board.numberOfChannels()).to.equal(8);
});
it('should be able to set ganglion mode', () => {
var board = new openBCIBoard.OpenBCIBoard({
boardType: 'ganglion'
});
(board.options.boardType).should.equal('ganglion');
});
it('should be able to set set daisy mode', () => {
var ourBoard1 = new openBCIBoard.OpenBCIBoard({
boardType: 'daisy'
});
var ourBoard2 = new openBCIBoard.OpenBCIBoard({
boardtype: 'daisy'
});
(ourBoard1.options.boardType).should.equal('daisy');
(ourBoard2.options.boardType).should.equal('daisy');
it('should get value for daisy',() => {
ourBoard1.sampleRate().should.equal(125);
});
it('should get value for daisy',() => {
ourBoard1.numberOfChannels().should.equal(16);
});
});
it('should be able to change baud rate', () => {
var ourBoard1 = new openBCIBoard.OpenBCIBoard({
baudRate: 9600
});
(ourBoard1.options.baudRate).should.equal(9600);
var ourBoard2 = new openBCIBoard.OpenBCIBoard({
baudrate: 9600
});
(ourBoard2.options.baudRate).should.equal(9600);
});
it('should be able to enter simulate mode from the constructor', () =>{
var board = new openBCIBoard.OpenBCIBoard({
simulate: true
});
expect(board.options.simulate).to.be.true;
});
it('should be able to set the simulator to board failure mode', () =>{
var ourBoard1 = new openBCIBoard.OpenBCIBoard({
simulatorBoardFailure: true
});
expect(ourBoard1.options.simulatorBoardFailure).to.be.true;
var ourBoard2 = new openBCIBoard.OpenBCIBoard({
simulatorboardfailure: true
});
expect(ourBoard2.options.simulatorBoardFailure).to.be.true;
});
it('should be able to attach the daisy board in the simulator', () =>{
var ourBoard1 = new openBCIBoard.OpenBCIBoard({
simulatorDaisyModuleAttached: true
});
expect(ourBoard1.options.simulatorDaisyModuleAttached).to.be.true;
// Verify multi case support
var ourBoard2 = new openBCIBoard.OpenBCIBoard({
simulatordaisymoduleattached: true
});
expect(ourBoard2.options.simulatorDaisyModuleAttached).to.be.true;
});
it('should be able to start the simulator with firmware version 2', () =>{
var ourBoard1 = new openBCIBoard.OpenBCIBoard({
simulatorFirmwareVersion: 'v2'
});
(ourBoard1.options.simulatorFirmwareVersion).should.equal(k.OBCIFirmwareV2);
// Verify multi case support
var ourBoard2 = new openBCIBoard.OpenBCIBoard({
simulatorfirmwareversion: 'v2'
});
(ourBoard2.options.simulatorFirmwareVersion).should.equal(k.OBCIFirmwareV2);
});
it('should be able to put the simulator in raw aux mode', () =>{
var ourBoard1 = new openBCIBoard.OpenBCIBoard({
simulatorHasAccelerometer: false
});
expect(ourBoard1.options.simulatorHasAccelerometer).to.be.false;
// Verify multi case support
var ourBoard2 = new openBCIBoard.OpenBCIBoard({
simulatorhasaccelerometer: false
});
expect(ourBoard2.options.simulatorHasAccelerometer).to.be.false;
});
it('should be able to make the internal clock of the simulator run slow', () =>{
var ourBoard1 = new openBCIBoard.OpenBCIBoard({
simulatorInternalClockDrift: -1
});
expect(ourBoard1.options.simulatorInternalClockDrift).to.be.lessThan(0);
// Verify multi case support
var ourBoard2 = new openBCIBoard.OpenBCIBoard({
simulatorinternalclockdrift: -1
});
expect(ourBoard2.options.simulatorInternalClockDrift).to.be.lessThan(0);
});
it('should be able to make the internal clock of the simulator run fast', () =>{
var ourBoard1 = new openBCIBoard.OpenBCIBoard({
simulatorInternalClockDrift: 1
});
expect(ourBoard1.options.simulatorInternalClockDrift).to.be.greaterThan(0);
// Verify multi case support
var ourBoard2 = new openBCIBoard.OpenBCIBoard({
simulatorinternalclockdrift: 1
});
expect(ourBoard2.options.simulatorInternalClockDrift).to.be.greaterThan(0);
});
it('should be able to not inject alpha waves into the simulator', function() {
var ourBoard1 = new openBCIBoard.OpenBCIBoard({
simulatorInjectAlpha: false
});
expect(ourBoard1.options.simulatorInjectAlpha).to.be.false;
// Verify multi case support
var ourBoard2 = new openBCIBoard.OpenBCIBoard({
simulatorinjectalpha: false
});
expect(ourBoard2.options.simulatorInjectAlpha).to.be.false;
});
it('can turn 50Hz line noise on', function() {
var ourBoard1 = new openBCIBoard.OpenBCIBoard({
simulatorInjectLineNoise: '50Hz'
});
expect(ourBoard1.options.simulatorInjectLineNoise).to.equal(k.OBCISimulatorLineNoiseHz50);
// Verify multi case support
var ourBoard2 = new openBCIBoard.OpenBCIBoard({
simulatorinjectlinenoise: '50Hz'
});
expect(ourBoard2.options.simulatorInjectLineNoise).to.equal(k.OBCISimulatorLineNoiseHz50);
});
it('can turn no line noise on', function() {
ourBoard = new openBCIBoard.OpenBCIBoard({
simulatorInjectLineNoise: 'None'
});
(ourBoard.options.simulatorInjectLineNoise).should.equal(k.OBCISimulatorLineNoiseNone);
});
it('defaults to 60Hz line noise when bad input', function() {
ourBoard = new openBCIBoard.OpenBCIBoard({
simulatorInjectLineNoise: '20Hz'
});
(ourBoard.options.simulatorInjectLineNoise).should.equal(k.OBCISimulatorLineNoiseHz60);
});
it('can enter simulate mode with different sample rate', function() {
ourBoard = new openBCIBoard.OpenBCIBoard({
simulate: true,
simulatorSampleRate: 69
});
(ourBoard.options.simulate).should.equal(true);
(ourBoard.options.simulatorSampleRate).should.equal(69);
(ourBoard.sampleRate()).should.equal(69);
});
it('should be able to attach the daisy board in the simulator', () =>{
var ourBoard1 = new openBCIBoard.OpenBCIBoard({
simulatorSerialPortFailure: true
});
expect(ourBoard1.options.simulatorSerialPortFailure).to.be.true;
// Verify multi case support
var ourBoard2 = new openBCIBoard.OpenBCIBoard({
simulatorserialportfailure: true
});
expect(ourBoard2.options.simulatorSerialPortFailure).to.be.true;
});
it('should be able to enter sync mode', function() {
var ourBoard1 = new openBCIBoard.OpenBCIBoard({
sntpTimeSync: true
});
expect(ourBoard1.options.sntpTimeSync).to.be.true;
// Verify multi case support
var ourBoard2 = new openBCIBoard.OpenBCIBoard({
sntptimesync: true
});
expect(ourBoard2.options.sntpTimeSync).to.be.true;
});
it('should be able to change the ntp pool host', function() {
var expectedPoolName = 'time.apple.com';
var ourBoard1 = new openBCIBoard.OpenBCIBoard({
sntpTimeSyncHost: expectedPoolName
});
expect(ourBoard1.options.sntpTimeSyncHost).to.equal(expectedPoolName);
// Verify multi case support
var ourBoard2 = new openBCIBoard.OpenBCIBoard({
sntptimesynchost: expectedPoolName
});
expect(ourBoard2.options.sntpTimeSyncHost).to.equal(expectedPoolName);
});
it('should be able to change the ntp pool port', function() {
var expectedPortNumber = 73;
var ourBoard1 = new openBCIBoard.OpenBCIBoard({
sntpTimeSyncPort: expectedPortNumber
});
expect(ourBoard1.options.sntpTimeSyncPort).to.equal(expectedPortNumber);
// Verify multi case support
var ourBoard2 = new openBCIBoard.OpenBCIBoard({
sntptimesyncport: expectedPortNumber
});
expect(ourBoard2.options.sntpTimeSyncPort).to.equal(expectedPortNumber);
});
it('can enter verbose mode', function() {
ourBoard = new openBCIBoard.OpenBCIBoard({
verbose: true
});
(ourBoard.options.verbose).should.equal(true);
});
it('should start in current stream state in the init mode', () => {
ourBoard = new openBCIBoard.OpenBCIBoard();
ourBoard.curParsingMode.should.equal(k.OBCIParsingReset);
});
it('configures impedance testing variables correctly', function() {
ourBoard = new openBCIBoard.OpenBCIBoard();
(ourBoard.impedanceTest.active).should.equal(false);
(ourBoard.impedanceTest.isTestingNInput).should.equal(false);
(ourBoard.impedanceTest.isTestingPInput).should.equal(false);
(ourBoard.impedanceTest.onChannel).should.equal(0);
(ourBoard.impedanceTest.sampleNumber).should.equal(0);
});
it('configures impedance array with the correct amount of channels for default', function() {
ourBoard = new openBCIBoard.OpenBCIBoard();
(ourBoard.impedanceArray.length).should.equal(8);
});
it('configures impedance array with the correct amount of channels for daisy', function() {
ourBoard = new openBCIBoard.OpenBCIBoard({
boardType: 'daisy'
});
(ourBoard.impedanceArray.length).should.equal(16);
});
it('configures impedance array with the correct amount of channels for ganglion', function() {
ourBoard = new openBCIBoard.OpenBCIBoard({
boardType: 'ganglion'
});
(ourBoard.impedanceArray.length).should.equal(4);
});
});
describe('#simulator', function() {
it('can enable simulator after constructor', function(done) {
ourBoard = new openBCIBoard.OpenBCIBoard({
verbose: true
});
ourBoard.simulatorEnable().should.be.fulfilled.and.notify(done);
});
it('should disable sim and call disconnected', function(done) {
ourBoard = new openBCIBoard.OpenBCIBoard({
verbose: true
});
var disconnectSpy = sinon.spy(ourBoard,"disconnect");
ourBoard.options.simulate.should.equal(false);
ourBoard.connected = true;
ourBoard.simulatorEnable().then(() => {
disconnectSpy.should.have.been.calledOnce;
ourBoard.options.simulate.should.equal(true);
done();
});
});
it('should not enable the simulator if already simulating', function(done) {
ourBoard = new openBCIBoard.OpenBCIBoard({
verbose: true,
simulate: true
});
ourBoard.simulatorEnable().should.be.rejected.and.notify(done);
});
it('can disable simulator', function(done) {
ourBoard = new openBCIBoard.OpenBCIBoard({
verbose: true,
simulate: true
});
ourBoard.simulatorDisable().should.be.fulfilled.and.notify(done);
});
it('should not disable simulator if not in simulate mode', function(done) {
ourBoard = new openBCIBoard.OpenBCIBoard({
verbose: true
});
ourBoard.simulatorDisable().should.be.rejected.and.notify(done);
});
it('should start sim and call disconnected', function(done) {
ourBoard = new openBCIBoard.OpenBCIBoard({
verbose: true,
simulate: true
});
var disconnectSpy = sinon.spy(ourBoard,"disconnect");
ourBoard.options.simulate.should.equal(true);
ourBoard.connected = true;
ourBoard.simulatorDisable().then(() => {
disconnectSpy.should.have.been.calledOnce;
ourBoard.options.simulate.should.equal(false);
done();
});
});
it('should be able to propagate constructor options to simulator', function(done) {
ourBoard = new openBCIBoard.OpenBCIBoard({
verbose: true,
simulate: true,
simulatorBoardFailure: true,
simulatorDaisyModuleAttached: true,
simulatorFirmwareVersion: k.OBCIFirmwareV2,
simulatorHasAccelerometer: false,
simulatorInternalClockDrift: -1,
simulatorInjectAlpha: false,
simulatorInjectLineNoise: 'None',
simulatorSampleRate: 16,
simulatorSerialPortFailure:true
});
ourBoard.connect(k.OBCISimulatorPortName)
.then(() => {
ourBoard.once('ready', () => {
var simOptions = ourBoard.serial.options;
expect(simOptions).to.be.an('object');
expect(simOptions.accel).to.be.false;
expect(simOptions.alpha).to.be.false;
expect(simOptions.boardFailure).to.be.true;
expect(simOptions.daisy).to.be.true;
expect(simOptions.drift).to.be.below(0);
expect(simOptions.firmwareVersion).to.be.equal(k.OBCIFirmwareV2);
expect(simOptions.lineNoise).to.be.equal(k.OBCISimulatorLineNoiseNone);
expect(simOptions.sampleRate).to.be.equal(16);
expect(simOptions.serialPortFailure).to.be.true;
expect(simOptions.verbose).to.be.true;
ourBoard.disconnect().then(done).catch(done);
});
}).catch(err => done(err));
});
});
describe('#boardTests', function() {
this.timeout(3000);
before(function() {
ourBoard = new openBCIBoard.OpenBCIBoard({
simulate: !realBoard,
verbose: true
});
spy = sinon.spy(ourBoard,"_writeAndDrain");
});
after(function(done) {
if (ourBoard.connected) {
ourBoard.disconnect().then(() => {
done();
});
} else {
done()
}
});
afterEach(function() {
if (spy) spy.reset();
});
describe('#connect/disconnect/streamStart/streamStop', function () {
it('gets the ready signal from the board and sends a stop streaming command before disconnecting', function(done) {
//spy = sinon.spy(ourBoard,"_writeAndDrain");
ourBoard.connect(masterPortName).catch(err => done(err));
ourBoard.once('ready', function() {
ourBoard.streamStart().catch(err => done(err)); // start streaming
ourBoard.once('sample',(sample) => { // wait till we get a sample
ourBoard.disconnect().then(() => { // call disconnect
//console.log('Device is streaming: ' + ourBoard.streaming ? 'true' : 'false');
setTimeout(() => {
spy.should.have.been.calledWithMatch(k.OBCIStreamStop);
var conditionalTimeout = realBoard ? 300 : 0;
setTimeout(() => {
done();
}, conditionalTimeout);
}, 4 * k.OBCIWriteIntervalDelayMSShort); // give plenty of time
}).catch(err => done(err));
});
});
});
it('rawDataPacket is emitted', function(done) {
ourBoard.connect(masterPortName).catch(err => done(err));
// for the ready signal test
ourBoard.once('ready', function() {
ourBoard.streamStart().catch(err => done(err)); // start streaming
ourBoard.once('rawDataPacket',(rawDataPacket) => { // wait till we get a raw data packet
ourBoard.disconnect().then(() => { // call disconnect
done();
}).catch(err => done(err));
});
});
});
});
describe('#write', function() {
before(function(done) {
if (ourBoard.connected) {
ourBoard.disconnect().then(() => {
done();
});
} else {
done()
}
});
it('rejects when not connected', function(done) {
ourBoard.write('b').should.be.rejected.and.notify(done);
});
});
// good
describe('#listPorts', function () {
it('returns a list of ports',function(done) {
ourBoard.listPorts().then(ports => {
if (ports.some(port => {
if (port.comName === masterPortName) {
return true;
}
})) {
done();
} else {
done();
}
});
})
});
describe('#sdStart',function() {
before(function(done) {
ourBoard.connect(k.OBCISimulatorPortName)
.then(() => {
ourBoard.once('ready',done);
})
.catch(err => done(err));
});
afterEach(function(done) {
ourBoard.sdStop()
.catch(done);
ourBoard.once('eot', () => {
done();
});
});
after(function(done) {
ourBoard.disconnect()
.then(done)
.catch(err => done(err));
});
it('can start 14 seconds of logging with sd',function(done) {
ourBoard.sdStart('14sec')
.catch(err => done(err));
ourBoard.once('eot', () => {
done();
});
});
it('can start 5 minutes of logging with sd',function(done) {
ourBoard.sdStart('5min')
.catch(err => done(err));
ourBoard.once('eot', () => {
done();
});
});
it('can start 15 minutes of logging with sd',function(done) {
ourBoard.sdStart('15min')
.catch(err => done(err));
ourBoard.once('eot', () => {
done();
});
});
it('can start 30 minutes of logging with sd',function(done) {
ourBoard.sdStart('30min')
.catch(err => done(err));
ourBoard.once('eot', () => {
done();
});
});
it('can start 1 hour of logging with sd',function(done) {
ourBoard.sdStart('1hour')
.catch(err => done(err));
ourBoard.once('eot', () => {
done();
});
});
it('can start 2 hours of logging with sd',function(done) {
ourBoard.sdStart('2hour')
.catch(err => done(err));
ourBoard.once('eot', () => {
done();
});
});
it('can start 4 hours of logging with sd',function(done) {
ourBoard.sdStart('4hour')
.catch(err => done(err));
ourBoard.once('eot', () => {
done();
});
});
it('can start 12 hours of logging with sd',function(done) {
ourBoard.sdStart('12hour')
.catch(err => done(err));
ourBoard.once('eot', () => {
done();
});
});
it('can start 24 hours of logging with sd',function(done) {
ourBoard.sdStart('24hour')
.catch(done);
ourBoard.once('eot', () => {
done();
});
});
});
describe('#sdStop',function() {
before(function(done) {
ourBoard.connect(k.OBCISimulatorPortName).catch(err => done(err));
ourBoard.once('ready',done);
});
it('can stop logging with sd',function(done) {
// console.log('yoyoyo');
ourBoard.sdStop()
.then(() => {
// console.log('taco');
spy.should.have.been.calledWith('j');
})
.catch(err => done(err));
ourBoard.once('eot', () => {
done();
});
});
});
// bad
describe('#channelOff', function () {
before(function(done) {
if (!ourBoard.connected) {
ourBoard.connect(masterPortName)
.then(done)
.catch(err => done(err));
} else {
done();
}
});
it('should call the write function with proper command for channel 1', function(done) {
ourBoard.channelOff(1).then(() => {
setTimeout(() => {
spy.should.have.been.calledWith(k.OBCIChannelOff_1);
done();
}, 5 * k.OBCIWriteIntervalDelayMSShort);
});
});
it('should call the write function with proper command for channel 16', function(done) {
//spy = sinon.spy(ourBoard,"_writeAndDrain");
ourBoard.channelOff(16).then(() => {
setTimeout(() => {
spy.should.have.been.calledWith(k.OBCIChannelOff_16);
done();
}, 5 * k.OBCIWriteIntervalDelayMSShort);
});
});
it('should reject with invalid channel', function(done) {
ourBoard.channelOff(0).should.be.rejected.and.notify(done);
});
it('should turn the realBoard channel off', function(done) {
ourBoard.channelOff(1).then(() => {
setTimeout(() => {
spy.should.have.been.calledWith(k.OBCIChannelOff_1);
done();
}, 5 * k.OBCIWriteIntervalDelayMSShort);
});
});
});
// good
describe('#channelOn', function () {
before(function(done) {
if (!ourBoard.connected) {
ourBoard.connect(masterPortName)
.then(done)
.catch(err => done(err));
} else {
done();
}
});
it('should call the write function with proper command for channel 2', function(done) {
ourBoard.channelOn(2).then(() => {
setTimeout(() => {
spy.should.have.been.calledWith(k.OBCIChannelOn_2);
done();
}, 5 * k.OBCIWriteIntervalDelayMSShort);
});
});
it('should call the write function with proper command for channel 16', function(done) {
ourBoard.channelOn(16).then(() => {
setTimeout(() => {
spy.should.have.been.calledWith(k.OBCIChannelOn_16);
done();
}, 5 * k.OBCIWriteIntervalDelayMSShort);
});
});
it('should reject with invalid channel', function(done) {
ourBoard.channelOn(0).should.be.rejected.and.notify(done);
});
});
// bad
describe('#channelSet', function () {
this.timeout(6000);
before(function(done) {
if (!ourBoard.connected) {
ourBoard.connect(masterPortName)
.then(done)
.catch(err => done(err));
} else {
done();
}
});
it('should call the writeAndDrain function array of commands 9 times', function(done) {
setTimeout(() => {
spy.reset();
ourBoard.channelSet(1,true,24,'normal',true,true,true)
.then(() => {
setTimeout(() => {
spy.callCount.should.equal(9);
done()
}, 15 * k.OBCIWriteIntervalDelayMSShort);
})
.catch(err => done(err));
}, 10 * k.OBCIWriteIntervalDelayMSShort); // give some time for writer to finish
});
it('should be rejected', function(done) {
ourBoard.channelSet(1,true,24,'normal','taco',true,true).should.be.rejected.and.notify(done);
});
});
describe('#impedanceTest Not Connected Rejects ',function() {
it('rejects all channeles when not streaming', function(done) {
ourBoard.impedanceTestAllChannels().should.be.rejected.and.notify(done);
});
it('rejects array channels when not streaming', function(done) {
ourBoard.impedanceTestChannels(['-','N','n','p','P','-','b','b']).should.be.rejected.and.notify(done);
});
});
describe('#impedancePrivates', function () {
describe('disconnected', function() {
before(function(done) {
if (ourBoard.connected) {
ourBoard.disconnect()
.then(done)
.catch(err => done(err));
} else {
done();
}
});
describe('#_impedanceTestSetChannel', function () {
it('should reject because not connected', function(done) {
ourBoard._impedanceTestSetChannel(0,false,false).should.be.rejected.and.notify(done);
});
});
});
});
});
/**
* Test the function that parses an incoming data buffer for packets
*/
describe('#_processDataBuffer', function() {
var _processQualifiedPacketSpy;
before(() => {
ourBoard = new openBCIBoard.OpenBCIBoard({
verbose: true
});
_processQualifiedPacketSpy = sinon.spy(ourBoard,"_processQualifiedPacket");
});
after(() => {
ourBoard = null;
});
it('should do nothing when empty buffer inserted', () => {
var buffer = null;
// Test the function
buffer = ourBoard._processDataBuffer(buffer);
expect(buffer).to.be.null;
});
it('should return an unaltered buffer if there is less than a packets worth of data in it', () => {
var expectedString = "AJ";
var buffer = new Buffer(expectedString);
// Reset the spy if it exists
if(_processQualifiedPacketSpy) _processQualifiedPacketSpy.reset();
// Test the function
buffer = ourBoard._processDataBuffer(buffer);
// Convert the buffer to a string and ensure that it equals the expected string
buffer.toString().should.equal(expectedString);
// Make sure that the spy was not infact called.
_processQualifiedPacketSpy.should.not.have.been.called;
});
it('should identify a packet',() => {
var buffer = openBCISample.samplePacketReal(0);
// Reset the spy if it exists
if(_processQualifiedPacketSpy) _processQualifiedPacketSpy.reset();
// Call the function under test
buffer = ourBoard._processDataBuffer(buffer);
// Ensure that we extracted only one buffer
_processQualifiedPacketSpy.should.have.been.calledOnce;
// The buffer should not have anything in it any more
expect(buffer).to.be.null;
});
it('should extract a buffer and preserve the remaining data in the buffer',() => {
var expectedString = "AJ";
var extraBuffer = new Buffer(expectedString);
// declare the big buffer
var buffer = new Buffer(k.OBCIPacketSize + extraBuffer.length);
// Fill that new big buffer with buffers
openBCISample.samplePacketReal(0).copy(buffer,0);
extraBuffer.copy(buffer,k.OBCIPacketSize);
// Reset the spy if it exists
if(_processQualifiedPacketSpy) _processQualifiedPacketSpy.reset();
// Call the function under test
buffer = ourBoard._processDataBuffer(buffer);
// Ensure that we extracted only one buffer
_processQualifiedPacketSpy.should.have.been.called;
// The buffer should have the epxected number of bytes left
buffer.length.should.equal(expectedString.length);
// Convert the buffer to a string and ensure that it equals the expected string
buffer.toString().should.equal(expectedString);
});
it('should be able to extract multiple packets from a single buffer',() => {
// We are going to extract multiple buffers
var expectedNumberOfBuffers = 3;
// declare the big buffer
var buffer = new Buffer(k.OBCIPacketSize * expectedNumberOfBuffers);
// Fill that new big buffer with buffers
openBCISample.samplePacketReal(0).copy(buffer,0);
openBCISample.samplePacketReal(1).copy(buffer,k.OBCIPacketSize);
openBCISample.samplePacketReal(2).copy(buffer,k.OBCIPacketSize * 2);
// Reset the spy if it exists
if(_processQualifiedPacketSpy) _processQualifiedPacketSpy.reset();
// Call the function under test
buffer = ourBoard._processDataBuffer(buffer);
// Ensure that we extracted only one buffer
_processQualifiedPacketSpy.should.have.been.calledThrice;
// The buffer should not have anything in it any more
expect(buffer).to.be.null;
});
it('should be able to get multiple packets and keep extra data on the end', () => {
var expectedString = "AJ";
var extraBuffer = new Buffer(expectedString);
// We are going to extract multiple buffers
var expectedNumberOfBuffers = 2;
// declare the big buffer
var buffer = new Buffer(k.OBCIPacketSize * expectedNumberOfBuffers + extraBuffer.length);
// Fill that new big buffer with buffers
openBCISample.samplePacketReal(0).copy(buffer,0);
openBCISample.samplePacketReal(1).copy(buffer,k.OBCIPacketSize);
extraBuffer.copy(buffer,k.OBCIPacketSize * 2);
// Reset the spy if it exists
if(_processQualifiedPacketSpy) _processQualifiedPacketSpy.reset();
// Call the function under test
buffer = ourBoard._processDataBuffer(buffer);
// Ensure that we extracted only one buffer
_processQualifiedPacketSpy.should.have.been.calledTwice;
// The buffer should not have anything in it any more
buffer.length.should.equal(extraBuffer.length);
});
it('should be able to get multiple packets with junk in the middle', () => {
var expectedString = ",";
var extraBuffer = new Buffer(expectedString);
// We are going to extract multiple buffers
var expectedNumberOfBuffers = 2;
// declare the big buffer
var buffer = new Buffer(k.OBCIPacketSize * expectedNumberOfBuffers + extraBuffer.length);
// Fill that new big buffer with buffers
openBCISample.samplePacketReal(0).copy(buffer,0);
extraBuffer.copy(buffer,k.OBCIPacketSize);
openBCISample.samplePacketReal(1).copy(buffer,k.OBCIPacketSize + extraBuffer.byteLength);
// Reset the spy if it exists
if(_processQualifiedPacketSpy) _processQualifiedPacketSpy.reset();
// Call the function under test
buffer = ourBoard._processDataBuffer(buffer);
// Ensure that we extracted only one buffer
_processQualifiedPacketSpy.should.have.been.calledTwice;
// The buffer should not have anything in it any more
bufferEqual(extraBuffer,buffer).should.be.true;
buffer.length.should.equal(extraBuffer.length);
});
it('should be able to get multiple packets with junk in the middle and end', () => {
var expectedString = ",";
var extraBuffer = new Buffer(expectedString);
// We are going to extract multiple buffers
var expectedNumberOfBuffers = 2;
// declare the big buffer
var buffer = new Buffer(k.OBCIPacketSize * expectedNumberOfBuffers + extraBuffer.length * 2);
// Fill that new big buffer with buffers
openBCISample.samplePacketReal(0).copy(buffer,0);
extraBuffer.copy(buffer,k.OBCIPacketSize);
openBCISample.samplePacketReal(1).copy(buffer,k.OBCIPacketSize + extraBuffer.byteLength);
extraBuffer.copy(buffer,k.OBCIPacketSize * 2 + extraBuffer.byteLength);
// Reset the spy if it exists
if(_processQualifiedPacketSpy) _processQualifiedPacketSpy.reset();
// Call the function under test
buffer = ourBoard._processDataBuffer(buffer);
// Ensure that we extracted only one buffer
_processQualifiedPacketSpy.should.have.been.calledTwice;
// The buffer should not have anything in it any more
bufferEqual(Buffer.concat([extraBuffer,extraBuffer],2),buffer).should.be.true;
buffer.length.should.equal(extraBuffer.length * 2);
});
});
/**
* Test the function that routes raw packets for processing
*/
describe('#_processQualifiedPacket', function() {
var ourBoard;
var funcSpyTimeSyncSet, funcSpyTimeSyncedAccel, funcSpyTimeSyncedRawAux, funcSpyStandardRawAux, funcSpyStandardAccel;
before(function() {
ourBoard = new openBCIBoard.OpenBCIBoard({
verbose: true
});
// Put watchers on all functions
funcSpyStandardAccel = sinon.spy(ourBoard,"_processPacketStandardAccel");
funcSpyStandardRawAux = sinon.spy(ourBoard,"_processPacketStandardRawAux");
funcSpyTimeSyncSet = sinon.spy(ourBoard,"_processPacketTimeSyncSet");
funcSpyTimeSyncedAccel = sinon.spy(ourBoard,"_processPacketTimeSyncedAccel");
funcSpyTimeSyncedRawAux = sinon.spy(ourBoard,"_processPacketTimeSyncedRawAux");
});
beforeEach(function() {
funcSpyStandardAccel.reset();
funcSpyStandardRawAux.reset();
funcSpyTimeSyncSet.reset();
funcSpyTimeSyncedAccel.reset();
funcSpyTimeSyncedRawAux.reset();
ourBoard.sync.curSyncObj = openBCISample.newSyncObject();
});
after(function() {
// ourBoard = null;
});
it('should process a standard packet',function() {
var buffer = openBCISample.samplePacket(0);
// Call the function under test
ourBoard._processQualifiedPacket(buffer);
// Ensure that we extracted only one buffer
funcSpyStandardAccel.should.have.been.calledOnce;
});
it('should process a standard packet with raw aux',function() {
var buffer = openBCISample.samplePacketStandardRawAux(0);
// Call the function under test
ourBoard._processQualifiedPacket(buffer);
// Ensure that we extracted only one buffer
funcSpyStandardRawAux.should.have.been.calledOnce;
});
it('should call nothing for a user defined packet type ',function() {
var buffer = openBCISample.samplePacketUserDefined();
// Call the function under test
ourBoard._processQualifiedPacket(buffer);
// Nothing should be called
funcSpyStandardAccel.should.not.have.been.called;
funcSpyStandardRawAux.should.not.have.been.called;
funcSpyTimeSyncSet.should.not.have.been.called;
funcSpyTimeSyncedAccel.should.not.have.been.called;
funcSpyTimeSyncedRawAux.should.not.have.been.called;
});
it('should process a time sync set packet with accel',function() {
var buffer = openBCISample.samplePacketAccelTimeSyncSet();
// Call the function under test
ourBoard._processQualifiedPacket(buffer);
// We should call to sync up
funcSpyTimeSyncSet.should.have.been.calledOnce;
funcSpyTimeSyncSet.should.have.been.calledWith(buffer);
// we should call to get a packet
funcSpyTimeSyncedAccel.should.have.been.calledOnce;
funcSpyTimeSyncedAccel.should.have.been.calledWith(buffer);
});
it('should process a time synced packet with accel',function() {
var buffer = openBCISample.samplePacketAccelTimeSynced(0);
// Call the function under test
ourBoard._processQualifiedPacket(buffer);
// Ensure that we extracted only one buffer
funcSpyTimeSyncedAccel.should.have.been.calledOnce;
});
it('should process a time sync set packet with raw aux',function() {
var buffer = openBCISample.samplePacketRawAuxTimeSyncSet(0);
// Call the function under test
ourBoard._processQualifiedPacket(buffer);
// We should call to sync up
funcSpyTimeSyncSet.should.have.been.calledOnce;
funcSpyTimeSyncSet.should.have.been.calledWith(buffer);
// we should call to get a packet
funcSpyTimeSyncedRawAux.should.have.been.calledOnce;
funcSpyTimeSyncedRawAux.should.have.been.calledWith(buffer);
});
it('should process a time synced packet with raw aux',function() {
var buffer = openBCISample.samplePacketRawAuxTimeSynced(0);
// Call the function under test
ourBoard._processQualifiedPacket(buffer);
// Ensure that we extracted only one buffer
funcSpyTimeSyncedRawAux.should.have.been.calledOnce;
});
it('should not identify any packet',function() {