@stoprocent/noble 1.19.0 → 2.0.0

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.
Files changed (97) hide show
  1. package/README.md +393 -650
  2. package/examples/advertisement-discovery.js +57 -48
  3. package/examples/connect-address.js +59 -34
  4. package/examples/echo.js +59 -69
  5. package/examples/enter-exit.js +55 -49
  6. package/examples/multiple-bindings.js +53 -0
  7. package/examples/peripheral-explorer-async.js +39 -21
  8. package/examples/peripheral-explorer.ts +52 -0
  9. package/index.d.ts +249 -209
  10. package/index.js +4 -1
  11. package/jest.config.js +4 -0
  12. package/lib/characteristic.js +153 -127
  13. package/lib/{win/src/callbacks.h → common/include/Emit.h} +17 -14
  14. package/lib/common/include/Peripheral.h +31 -0
  15. package/lib/common/include/ThreadSafeCallback.h +95 -0
  16. package/lib/{win/src/callbacks.cc → common/src/Emit.cc} +111 -68
  17. package/lib/descriptor.js +57 -54
  18. package/lib/hci-socket/acl-stream.js +2 -4
  19. package/lib/hci-socket/bindings.js +96 -73
  20. package/lib/hci-socket/gap.js +2 -3
  21. package/lib/hci-socket/gatt.js +2 -5
  22. package/lib/hci-socket/hci.js +19 -7
  23. package/lib/hci-socket/signaling.js +2 -3
  24. package/lib/hci-socket/smp.js +2 -3
  25. package/lib/hci-socket/vs.js +1 -0
  26. package/lib/mac/binding.gyp +5 -7
  27. package/lib/mac/bindings.js +1 -3
  28. package/lib/mac/src/ble_manager.h +1 -8
  29. package/lib/mac/src/ble_manager.mm +87 -44
  30. package/lib/mac/src/napi_objc.h +1 -0
  31. package/lib/mac/src/napi_objc.mm +0 -6
  32. package/lib/mac/src/noble_mac.h +5 -3
  33. package/lib/mac/src/noble_mac.mm +99 -57
  34. package/lib/mac/src/objc_cpp.h +3 -2
  35. package/lib/mac/src/objc_cpp.mm +0 -6
  36. package/lib/noble.js +579 -488
  37. package/lib/peripheral.js +171 -174
  38. package/lib/resolve-bindings.js +37 -30
  39. package/lib/service.js +58 -55
  40. package/lib/win/binding.gyp +4 -11
  41. package/lib/win/bindings.js +1 -3
  42. package/lib/win/src/ble_manager.cc +291 -166
  43. package/lib/win/src/ble_manager.h +11 -13
  44. package/lib/win/src/napi_winrt.cc +1 -7
  45. package/lib/win/src/napi_winrt.h +1 -1
  46. package/lib/win/src/noble_winrt.cc +88 -61
  47. package/lib/win/src/noble_winrt.h +5 -3
  48. package/lib/win/src/notify_map.cc +0 -7
  49. package/lib/win/src/notify_map.h +1 -8
  50. package/lib/win/src/peripheral_winrt.cc +29 -11
  51. package/lib/win/src/peripheral_winrt.h +1 -1
  52. package/lib/win/src/radio_watcher.cc +79 -69
  53. package/lib/win/src/radio_watcher.h +30 -11
  54. package/lib/win/src/winrt_cpp.cc +1 -1
  55. package/lib/win/src/winrt_cpp.h +3 -0
  56. package/package.json +14 -17
  57. package/prebuilds/darwin-x64+arm64/@stoprocent+noble.node +0 -0
  58. package/prebuilds/win32-ia32/@stoprocent+noble.node +0 -0
  59. package/prebuilds/win32-x64/@stoprocent+noble.node +0 -0
  60. package/test/lib/characteristic.test.js +202 -322
  61. package/test/lib/descriptor.test.js +62 -95
  62. package/test/lib/hci-socket/acl-stream.test.js +112 -108
  63. package/test/lib/hci-socket/bindings.test.js +576 -365
  64. package/test/lib/hci-socket/hci.test.js +442 -473
  65. package/test/lib/hci-socket/signaling.test.js +45 -48
  66. package/test/lib/hci-socket/smp.test.js +144 -142
  67. package/test/lib/hci-socket/vs.test.js +193 -18
  68. package/test/lib/peripheral.test.js +492 -322
  69. package/test/lib/resolve-bindings.test.js +207 -82
  70. package/test/lib/service.test.js +79 -88
  71. package/test/noble.test.js +381 -1085
  72. package/.editorconfig +0 -11
  73. package/.nycrc.json +0 -4
  74. package/codecov.yml +0 -5
  75. package/examples/cache-gatt-discovery.js +0 -198
  76. package/examples/cache-gatt-reconnect.js +0 -164
  77. package/examples/ext-advertisement-discovery.js +0 -65
  78. package/examples/peripheral-explorer.js +0 -225
  79. package/examples/pizza/central.js +0 -194
  80. package/examples/pizza/pizza.js +0 -60
  81. package/examples/test/test.custom.js +0 -131
  82. package/examples/uart-bind-params.js +0 -28
  83. package/lib/distributed/bindings.js +0 -326
  84. package/lib/mac/src/callbacks.cc +0 -222
  85. package/lib/mac/src/callbacks.h +0 -84
  86. package/lib/mac/src/peripheral.h +0 -23
  87. package/lib/resolve-bindings-web.js +0 -9
  88. package/lib/webbluetooth/bindings.js +0 -368
  89. package/lib/websocket/bindings.js +0 -321
  90. package/lib/win/src/peripheral.h +0 -23
  91. package/test/lib/distributed/bindings.test.js +0 -918
  92. package/test/lib/webbluetooth/bindings.test.js +0 -190
  93. package/test/lib/websocket/bindings.test.js +0 -456
  94. package/test/mocha.setup.js +0 -0
  95. package/with-bindings.js +0 -5
  96. package/with-custom-binding.js +0 -6
  97. package/ws-slave.js +0 -404
@@ -1,94 +1,91 @@
1
- const should = require('should');
2
- const sinon = require('sinon');
3
- const proxyquire = require('proxyquire').noCallThru();
1
+ // Mock the os module
2
+ jest.mock('os', () => ({
3
+ platform: jest.fn()
4
+ }));
4
5
 
5
- const fakeOs = {};
6
- const { assert } = sinon;
7
- const Signaling = proxyquire('../../../lib/hci-socket/signaling', { os: fakeOs });
6
+ const os = require('os');
7
+ const Signaling = require('../../../lib/hci-socket/signaling');
8
8
 
9
9
  describe('hci-socket signaling', () => {
10
10
  let signaling;
11
-
11
+ let aclStream;
12
12
  const handle = 'handle';
13
- const aclStream = {
14
- on: sinon.spy(),
15
- removeListener: sinon.spy(),
16
- write: sinon.spy()
17
- };
18
13
 
19
14
  beforeEach(() => {
20
- signaling = new Signaling(handle, aclStream);
21
- });
15
+ // Reset mocks
16
+ jest.clearAllMocks();
17
+
18
+ // Setup aclStream mock
19
+ aclStream = {
20
+ on: jest.fn(),
21
+ removeListener: jest.fn(),
22
+ write: jest.fn()
23
+ };
22
24
 
23
- afterEach(() => {
24
- sinon.reset();
25
+ signaling = new Signaling(handle, aclStream);
25
26
  });
26
27
 
27
- it('construct', () => {
28
- assert.callCount(aclStream.on, 2);
29
- assert.calledWithMatch(aclStream.on, 'data', sinon.match.func);
30
- assert.calledWithMatch(aclStream.on, 'end', sinon.match.func);
28
+ test('construct', () => {
29
+ expect(aclStream.on).toHaveBeenCalledTimes(2);
30
+ expect(aclStream.on).toHaveBeenCalledWith('data', expect.any(Function));
31
+ expect(aclStream.on).toHaveBeenCalledWith('end', expect.any(Function));
31
32
 
32
- should(signaling._handle).equal(handle);
33
- should(signaling._aclStream).equal(aclStream);
33
+ expect(signaling._handle).toBe(handle);
34
+ expect(signaling._aclStream).toBe(aclStream);
34
35
  });
35
36
 
36
37
  describe('onAclStreamData', () => {
37
38
  beforeEach(() => {
38
- signaling.processConnectionParameterUpdateRequest = sinon.spy();
39
- });
40
-
41
- afterEach(() => {
42
- sinon.reset();
39
+ signaling.processConnectionParameterUpdateRequest = jest.fn();
43
40
  });
44
41
 
45
- it('should do nothing as not SIGNALING_CID', () => {
42
+ test('should do nothing as not SIGNALING_CID', () => {
46
43
  signaling.onAclStreamData(0, 'data');
47
- assert.notCalled(signaling.processConnectionParameterUpdateRequest);
44
+ expect(signaling.processConnectionParameterUpdateRequest).not.toHaveBeenCalled();
48
45
  });
49
46
 
50
- it('should do nothing as not CONNECTION_PARAMETER_UPDATE_REQUEST', () => {
47
+ test('should do nothing as not CONNECTION_PARAMETER_UPDATE_REQUEST', () => {
51
48
  const data = Buffer.from([0, 1, 2, 3, 4]);
52
49
  signaling.onAclStreamData(5, data);
53
- assert.notCalled(signaling.processConnectionParameterUpdateRequest);
50
+ expect(signaling.processConnectionParameterUpdateRequest).not.toHaveBeenCalled();
54
51
  });
55
52
 
56
- it('should do nothing processConnectionParameterUpdateRequest', () => {
53
+ test('should call processConnectionParameterUpdateRequest', () => {
57
54
  const data = Buffer.from([18, 1, 2, 3, 4, 5]);
58
55
  signaling.onAclStreamData(5, data);
59
- assert.calledOnceWithExactly(signaling.processConnectionParameterUpdateRequest, 1, Buffer.from([4, 5]));
56
+ expect(signaling.processConnectionParameterUpdateRequest).toHaveBeenCalledWith(1, Buffer.from([4, 5]));
60
57
  });
61
58
  });
62
59
 
63
- it('onAclStreamEnd', () => {
60
+ test('onAclStreamEnd', () => {
64
61
  signaling.onAclStreamEnd();
65
62
 
66
- assert.callCount(aclStream.removeListener, 2);
67
- assert.calledWithMatch(aclStream.removeListener, 'data', sinon.match.func);
68
- assert.calledWithMatch(aclStream.removeListener, 'end', sinon.match.func);
63
+ expect(aclStream.removeListener).toHaveBeenCalledTimes(2);
64
+ expect(aclStream.removeListener).toHaveBeenCalledWith('data', expect.any(Function));
65
+ expect(aclStream.removeListener).toHaveBeenCalledWith('end', expect.any(Function));
69
66
  });
70
67
 
71
68
  describe('processConnectionParameterUpdateRequest', () => {
72
- it('should not write on linux', () => {
73
- fakeOs.platform = sinon.fake.returns('linux');
74
- const callback = sinon.spy();
69
+ test('should not write on linux', () => {
70
+ os.platform.mockReturnValue('linux');
71
+ const callback = jest.fn();
75
72
 
76
73
  signaling.on('connectionParameterUpdateRequest', callback);
77
74
  signaling.processConnectionParameterUpdateRequest(1, Buffer.from([1, 0, 2, 0, 3, 0, 4, 0]));
78
75
 
79
- assert.notCalled(callback);
80
- assert.notCalled(aclStream.write);
76
+ expect(callback).not.toHaveBeenCalled();
77
+ expect(aclStream.write).not.toHaveBeenCalled();
81
78
  });
82
79
 
83
- it('should write on !linux', () => {
84
- fakeOs.platform = sinon.fake.returns('!linux');
85
- const callback = sinon.spy();
80
+ test('should write on !linux', () => {
81
+ os.platform.mockReturnValue('!linux');
82
+ const callback = jest.fn();
86
83
 
87
84
  signaling.on('connectionParameterUpdateRequest', callback);
88
85
  signaling.processConnectionParameterUpdateRequest(1, Buffer.from([1, 0, 2, 0, 3, 0, 4, 0]));
89
86
 
90
- assert.calledOnceWithExactly(callback, handle, 1.25, 2.5, 3, 40);
91
- assert.calledOnceWithExactly(aclStream.write, 5, Buffer.from([0x13, 0x01, 0x02, 0x00, 0x00, 0x00]));
87
+ expect(callback).toHaveBeenCalledWith(handle, 1.25, 2.5, 3, 40);
88
+ expect(aclStream.write).toHaveBeenCalledWith(5, Buffer.from([0x13, 0x01, 0x02, 0x00, 0x00, 0x00]));
92
89
  });
93
90
  });
94
- });
91
+ });
@@ -1,268 +1,270 @@
1
- const should = require('should');
2
- const sinon = require('sinon');
3
- const proxyquire = require('proxyquire').noCallThru();
1
+ // Mock the crypto module
2
+ jest.mock('../../../lib/hci-socket/crypto', () => ({
3
+ r: jest.fn(),
4
+ c1: jest.fn(),
5
+ s1: jest.fn()
6
+ }));
4
7
 
5
- const crypto = {};
6
- const { assert } = sinon;
7
- const Smp = proxyquire('../../../lib/hci-socket/smp', { './crypto': crypto });
8
+ // Import the mocked modules
9
+ const crypto = require('../../../lib/hci-socket/crypto');
10
+ const Smp = require('../../../lib/hci-socket/smp');
8
11
 
9
12
  describe('hci-socket smp', () => {
10
13
  let smp;
11
-
12
- const aclStream = {};
14
+ let aclStream;
15
+
13
16
  const localAddressType = 'public';
14
17
  const localAddress = 'aa:bb:cc:dd:ee:ff';
15
18
  const remoteAddressType = 'random';
16
19
  const remoteAddress = '00:11:22:33:44:55';
17
20
 
18
21
  beforeEach(() => {
19
- aclStream.on = sinon.spy();
20
- aclStream.removeListener = sinon.spy();
21
- aclStream.write = sinon.spy();
22
+ aclStream = {
23
+ on: jest.fn(),
24
+ removeListener: jest.fn(),
25
+ write: jest.fn()
26
+ };
22
27
 
23
28
  smp = new Smp(aclStream, localAddressType, localAddress, remoteAddressType, remoteAddress);
24
29
  });
25
30
 
26
31
  afterEach(() => {
27
- sinon.reset();
32
+ jest.clearAllMocks();
28
33
  });
29
34
 
30
- it('construct 1', () => {
31
- assert.callCount(aclStream.on, 2);
32
- assert.calledWithMatch(aclStream.on, 'data', sinon.match.func);
33
- assert.calledWithMatch(aclStream.on, 'end', sinon.match.func);
35
+ test('construct 1', () => {
36
+ expect(aclStream.on).toHaveBeenCalledTimes(2);
37
+ expect(aclStream.on).toHaveBeenCalledWith('data', expect.any(Function));
38
+ expect(aclStream.on).toHaveBeenCalledWith('end', expect.any(Function));
34
39
 
35
- should(smp._iat).deepEqual(Buffer.from([0x00]));
36
- should(smp._ia).deepEqual(Buffer.from([0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa]));
37
- should(smp._rat).deepEqual(Buffer.from([0x01]));
38
- should(smp._ra).deepEqual(Buffer.from([0x55, 0x44, 0x33, 0x22, 0x11, 0x00]));
40
+ expect(smp._iat).toEqual(Buffer.from([0x00]));
41
+ expect(smp._ia).toEqual(Buffer.from([0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa]));
42
+ expect(smp._rat).toEqual(Buffer.from([0x01]));
43
+ expect(smp._ra).toEqual(Buffer.from([0x55, 0x44, 0x33, 0x22, 0x11, 0x00]));
39
44
  });
40
45
 
41
- it('construct 2', () => {
42
- sinon.reset(aclStream);
46
+ test('construct 2', () => {
47
+ jest.clearAllMocks();
43
48
  const smp = new Smp(aclStream, remoteAddressType, remoteAddress, localAddressType, localAddress);
44
49
 
45
- assert.callCount(aclStream.on, 2);
46
- assert.calledWithMatch(aclStream.on, 'data', sinon.match.func);
47
- assert.calledWithMatch(aclStream.on, 'end', sinon.match.func);
50
+ expect(aclStream.on).toHaveBeenCalledTimes(2);
51
+ expect(aclStream.on).toHaveBeenCalledWith('data', expect.any(Function));
52
+ expect(aclStream.on).toHaveBeenCalledWith('end', expect.any(Function));
48
53
 
49
- should(smp._iat).deepEqual(Buffer.from([0x01]));
50
- should(smp._ia).deepEqual(Buffer.from([0x55, 0x44, 0x33, 0x22, 0x11, 0x00]));
51
- should(smp._rat).deepEqual(Buffer.from([0x00]));
52
- should(smp._ra).deepEqual(Buffer.from([0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa]));
54
+ expect(smp._iat).toEqual(Buffer.from([0x01]));
55
+ expect(smp._ia).toEqual(Buffer.from([0x55, 0x44, 0x33, 0x22, 0x11, 0x00]));
56
+ expect(smp._rat).toEqual(Buffer.from([0x00]));
57
+ expect(smp._ra).toEqual(Buffer.from([0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa]));
53
58
  });
54
59
 
55
- it('should write sendPairingRequest', () => {
56
- smp.write = sinon.spy();
60
+ test('should write sendPairingRequest', () => {
61
+ smp.write = jest.fn();
57
62
 
58
63
  smp.sendPairingRequest();
59
64
 
60
- assert.calledOnceWithExactly(smp.write, Buffer.from([0x01, 0x03, 0x00, 0x01, 0x10, 0x00, 0x01]));
65
+ expect(smp.write).toHaveBeenCalledWith(Buffer.from([0x01, 0x03, 0x00, 0x01, 0x10, 0x00, 0x01]));
61
66
  });
62
67
 
63
68
  describe('onAclStreamData', () => {
64
69
  beforeEach(() => {
65
- smp.handlePairingResponse = sinon.spy();
66
- smp.handlePairingConfirm = sinon.spy();
67
- smp.handlePairingRandom = sinon.spy();
68
- smp.handlePairingFailed = sinon.spy();
69
- smp.handleEncryptInfo = sinon.spy();
70
- smp.handleMasterIdent = sinon.spy();
70
+ smp.handlePairingResponse = jest.fn();
71
+ smp.handlePairingConfirm = jest.fn();
72
+ smp.handlePairingRandom = jest.fn();
73
+ smp.handlePairingFailed = jest.fn();
74
+ smp.handleEncryptInfo = jest.fn();
75
+ smp.handleMasterIdent = jest.fn();
71
76
  });
72
77
 
73
- it('should do nothing with !SMP_CID', () => {
78
+ test('should do nothing with !SMP_CID', () => {
74
79
  smp.onAclStreamData(0);
75
80
 
76
- assert.notCalled(smp.handlePairingResponse);
77
- assert.notCalled(smp.handlePairingConfirm);
78
- assert.notCalled(smp.handlePairingRandom);
79
- assert.notCalled(smp.handlePairingFailed);
80
- assert.notCalled(smp.handleEncryptInfo);
81
- assert.notCalled(smp.handleMasterIdent);
81
+ expect(smp.handlePairingResponse).not.toHaveBeenCalled();
82
+ expect(smp.handlePairingConfirm).not.toHaveBeenCalled();
83
+ expect(smp.handlePairingRandom).not.toHaveBeenCalled();
84
+ expect(smp.handlePairingFailed).not.toHaveBeenCalled();
85
+ expect(smp.handleEncryptInfo).not.toHaveBeenCalled();
86
+ expect(smp.handleMasterIdent).not.toHaveBeenCalled();
82
87
  });
83
88
 
84
- it('should handlePairingResponse', () => {
89
+ test('should handlePairingResponse', () => {
85
90
  const data = Buffer.from([0x02, 0x33, 0x44]);
86
91
  smp.onAclStreamData(6, data);
87
92
 
88
- assert.calledOnceWithExactly(smp.handlePairingResponse, data);
89
- assert.notCalled(smp.handlePairingConfirm);
90
- assert.notCalled(smp.handlePairingRandom);
91
- assert.notCalled(smp.handlePairingFailed);
92
- assert.notCalled(smp.handleEncryptInfo);
93
- assert.notCalled(smp.handleMasterIdent);
93
+ expect(smp.handlePairingResponse).toHaveBeenCalledWith(data);
94
+ expect(smp.handlePairingConfirm).not.toHaveBeenCalled();
95
+ expect(smp.handlePairingRandom).not.toHaveBeenCalled();
96
+ expect(smp.handlePairingFailed).not.toHaveBeenCalled();
97
+ expect(smp.handleEncryptInfo).not.toHaveBeenCalled();
98
+ expect(smp.handleMasterIdent).not.toHaveBeenCalled();
94
99
  });
95
100
 
96
- it('should handlePairingConfirm', () => {
101
+ test('should handlePairingConfirm', () => {
97
102
  const data = Buffer.from([0x03, 0x33, 0x44]);
98
103
  smp.onAclStreamData(6, data);
99
104
 
100
- assert.notCalled(smp.handlePairingResponse);
101
- assert.calledOnceWithExactly(smp.handlePairingConfirm, data);
102
- assert.notCalled(smp.handlePairingRandom);
103
- assert.notCalled(smp.handlePairingFailed);
104
- assert.notCalled(smp.handleEncryptInfo);
105
- assert.notCalled(smp.handleMasterIdent);
105
+ expect(smp.handlePairingResponse).not.toHaveBeenCalled();
106
+ expect(smp.handlePairingConfirm).toHaveBeenCalledWith(data);
107
+ expect(smp.handlePairingRandom).not.toHaveBeenCalled();
108
+ expect(smp.handlePairingFailed).not.toHaveBeenCalled();
109
+ expect(smp.handleEncryptInfo).not.toHaveBeenCalled();
110
+ expect(smp.handleMasterIdent).not.toHaveBeenCalled();
106
111
  });
107
112
 
108
- it('should handlePairingRandom', () => {
113
+ test('should handlePairingRandom', () => {
109
114
  const data = Buffer.from([0x04, 0x33, 0x44]);
110
115
  smp.onAclStreamData(6, data);
111
116
 
112
- assert.notCalled(smp.handlePairingResponse);
113
- assert.notCalled(smp.handlePairingConfirm);
114
- assert.calledOnceWithExactly(smp.handlePairingRandom, data);
115
- assert.notCalled(smp.handlePairingFailed);
116
- assert.notCalled(smp.handleEncryptInfo);
117
- assert.notCalled(smp.handleMasterIdent);
117
+ expect(smp.handlePairingResponse).not.toHaveBeenCalled();
118
+ expect(smp.handlePairingConfirm).not.toHaveBeenCalled();
119
+ expect(smp.handlePairingRandom).toHaveBeenCalledWith(data);
120
+ expect(smp.handlePairingFailed).not.toHaveBeenCalled();
121
+ expect(smp.handleEncryptInfo).not.toHaveBeenCalled();
122
+ expect(smp.handleMasterIdent).not.toHaveBeenCalled();
118
123
  });
119
124
 
120
- it('should handlePairingFailed', () => {
125
+ test('should handlePairingFailed', () => {
121
126
  const data = Buffer.from([0x05, 0x33, 0x44]);
122
127
  smp.onAclStreamData(6, data);
123
128
 
124
- assert.notCalled(smp.handlePairingResponse);
125
- assert.notCalled(smp.handlePairingConfirm);
126
- assert.notCalled(smp.handlePairingRandom);
127
- assert.calledOnceWithExactly(smp.handlePairingFailed, data);
128
- assert.notCalled(smp.handleEncryptInfo);
129
- assert.notCalled(smp.handleMasterIdent);
129
+ expect(smp.handlePairingResponse).not.toHaveBeenCalled();
130
+ expect(smp.handlePairingConfirm).not.toHaveBeenCalled();
131
+ expect(smp.handlePairingRandom).not.toHaveBeenCalled();
132
+ expect(smp.handlePairingFailed).toHaveBeenCalledWith(data);
133
+ expect(smp.handleEncryptInfo).not.toHaveBeenCalled();
134
+ expect(smp.handleMasterIdent).not.toHaveBeenCalled();
130
135
  });
131
136
 
132
- it('should handleEncryptInfo', () => {
137
+ test('should handleEncryptInfo', () => {
133
138
  const data = Buffer.from([0x06, 0x33, 0x44]);
134
139
  smp.onAclStreamData(6, data);
135
140
 
136
- assert.notCalled(smp.handlePairingResponse);
137
- assert.notCalled(smp.handlePairingConfirm);
138
- assert.notCalled(smp.handlePairingRandom);
139
- assert.notCalled(smp.handlePairingFailed);
140
- assert.calledOnceWithExactly(smp.handleEncryptInfo, data);
141
- assert.notCalled(smp.handleMasterIdent);
141
+ expect(smp.handlePairingResponse).not.toHaveBeenCalled();
142
+ expect(smp.handlePairingConfirm).not.toHaveBeenCalled();
143
+ expect(smp.handlePairingRandom).not.toHaveBeenCalled();
144
+ expect(smp.handlePairingFailed).not.toHaveBeenCalled();
145
+ expect(smp.handleEncryptInfo).toHaveBeenCalledWith(data);
146
+ expect(smp.handleMasterIdent).not.toHaveBeenCalled();
142
147
  });
143
148
 
144
- it('should handleMasterIdent', () => {
149
+ test('should handleMasterIdent', () => {
145
150
  const data = Buffer.from([0x07, 0x33, 0x44]);
146
151
  smp.onAclStreamData(6, data);
147
152
 
148
- assert.notCalled(smp.handlePairingResponse);
149
- assert.notCalled(smp.handlePairingConfirm);
150
- assert.notCalled(smp.handlePairingRandom);
151
- assert.notCalled(smp.handlePairingFailed);
152
- assert.notCalled(smp.handleEncryptInfo);
153
- assert.calledOnceWithExactly(smp.handleMasterIdent, data);
153
+ expect(smp.handlePairingResponse).not.toHaveBeenCalled();
154
+ expect(smp.handlePairingConfirm).not.toHaveBeenCalled();
155
+ expect(smp.handlePairingRandom).not.toHaveBeenCalled();
156
+ expect(smp.handlePairingFailed).not.toHaveBeenCalled();
157
+ expect(smp.handleEncryptInfo).not.toHaveBeenCalled();
158
+ expect(smp.handleMasterIdent).toHaveBeenCalledWith(data);
154
159
  });
155
160
 
156
- it('should do nothing on bad code', () => {
161
+ test('should do nothing on bad code', () => {
157
162
  const data = Buffer.from([0x08, 0x33, 0x44]);
158
163
  smp.onAclStreamData(6, data);
159
164
 
160
- assert.notCalled(smp.handlePairingResponse);
161
- assert.notCalled(smp.handlePairingConfirm);
162
- assert.notCalled(smp.handlePairingRandom);
163
- assert.notCalled(smp.handlePairingFailed);
164
- assert.notCalled(smp.handleEncryptInfo);
165
- assert.notCalled(smp.handleMasterIdent);
165
+ expect(smp.handlePairingResponse).not.toHaveBeenCalled();
166
+ expect(smp.handlePairingConfirm).not.toHaveBeenCalled();
167
+ expect(smp.handlePairingRandom).not.toHaveBeenCalled();
168
+ expect(smp.handlePairingFailed).not.toHaveBeenCalled();
169
+ expect(smp.handleEncryptInfo).not.toHaveBeenCalled();
170
+ expect(smp.handleMasterIdent).not.toHaveBeenCalled();
166
171
  });
167
172
  });
168
173
 
169
- it('onAclStreamEnd', () => {
170
- const callback = sinon.spy();
174
+ test('onAclStreamEnd', () => {
175
+ const callback = jest.fn();
171
176
  smp.on('end', callback);
172
177
  smp.onAclStreamEnd();
173
178
 
174
- assert.callCount(aclStream.removeListener, 2);
175
- assert.calledWithMatch(aclStream.removeListener, 'data', sinon.match.func);
176
- assert.calledWithMatch(aclStream.removeListener, 'end', sinon.match.func);
177
- assert.calledOnceWithExactly(callback);
179
+ expect(aclStream.removeListener).toHaveBeenCalledTimes(2);
180
+ expect(aclStream.removeListener).toHaveBeenCalledWith('data', expect.any(Function));
181
+ expect(aclStream.removeListener).toHaveBeenCalledWith('end', expect.any(Function));
182
+ expect(callback).toHaveBeenCalled();
178
183
  });
179
184
 
180
- it('handlePairingResponse', () => {
181
- smp.write = sinon.spy();
182
- crypto.r = sinon.spy();
183
- crypto.c1 = sinon.fake.returns(Buffer.from([0x99]));
185
+ test('handlePairingResponse', () => {
186
+ smp.write = jest.fn();
187
+ crypto.c1.mockReturnValue(Buffer.from([0x99]));
184
188
 
185
189
  smp.handlePairingResponse('data');
186
190
 
187
- should(smp._pres).equal('data');
188
-
189
- assert.calledOnceWithExactly(crypto.r);
190
- assert.calledOnce(crypto.c1);
191
- assert.calledOnceWithExactly(smp.write, Buffer.from([0x03, 0x99]));
191
+ expect(smp._pres).toBe('data');
192
+ expect(crypto.r).toHaveBeenCalled();
193
+ expect(crypto.c1).toHaveBeenCalled();
194
+ expect(smp.write).toHaveBeenCalledWith(Buffer.from([0x03, 0x99]));
192
195
  });
193
196
 
194
- it('handlePairingConfirm', () => {
195
- smp.write = sinon.spy();
197
+ test('handlePairingConfirm', () => {
198
+ smp.write = jest.fn();
196
199
  smp._r = Buffer.from([0x99]);
197
200
 
198
201
  smp.handlePairingConfirm('data');
199
202
 
200
- should(smp._pcnf).equal('data');
201
-
202
- assert.calledOnceWithExactly(smp.write, Buffer.from([0x04, 0x99]));
203
+ expect(smp._pcnf).toBe('data');
204
+ expect(smp.write).toHaveBeenCalledWith(Buffer.from([0x04, 0x99]));
203
205
  });
204
206
 
205
207
  describe('handlePairingRandom', () => {
206
- it('should emit stk', () => {
207
- crypto.c1 = sinon.fake.returns(Buffer.from([0x99]));
208
- crypto.s1 = sinon.fake.returns('stk_answer');
208
+ test('should emit stk', () => {
209
+ crypto.c1.mockReturnValue(Buffer.from([0x99]));
210
+ crypto.s1.mockReturnValue('stk_answer');
209
211
 
210
212
  const data = Buffer.from([0, 1]);
211
- const callback = sinon.spy();
212
- const failCallback = sinon.spy();
213
+ const callback = jest.fn();
214
+ const failCallback = jest.fn();
213
215
 
214
216
  smp._pcnf = Buffer.from([3, 153]);
215
217
  smp.on('stk', callback);
216
218
  smp.on('fail', failCallback);
217
219
  smp.handlePairingRandom(data);
218
220
 
219
- assert.calledOnceWithExactly(callback, 'stk_answer');
220
- assert.notCalled(failCallback);
221
+ expect(callback).toHaveBeenCalledWith('stk_answer');
222
+ expect(failCallback).not.toHaveBeenCalled();
221
223
  });
222
224
 
223
- it('should write and emit fail stk', () => {
224
- crypto.c1 = sinon.fake.returns(Buffer.from([0x99]));
225
- crypto.s1 = sinon.fake.returns('stk_answer');
225
+ test('should write and emit fail stk', () => {
226
+ crypto.c1.mockReturnValue(Buffer.from([0x99]));
227
+ crypto.s1.mockReturnValue('stk_answer');
226
228
 
227
229
  const data = Buffer.from([0, 1]);
228
- const callback = sinon.spy();
229
- const failCallback = sinon.spy();
230
+ const callback = jest.fn();
231
+ const failCallback = jest.fn();
230
232
 
231
- smp.write = sinon.spy();
233
+ smp.write = jest.fn();
232
234
  smp._pcnf = Buffer.from([0]);
233
235
  smp.on('stk', callback);
234
236
  smp.on('fail', failCallback);
235
237
  smp.handlePairingRandom(data);
236
238
 
237
- assert.calledOnceWithExactly(smp.write, Buffer.from([4, 3]));
238
- assert.notCalled(callback);
239
- assert.calledOnceWithExactly(failCallback);
239
+ expect(smp.write).toHaveBeenCalledWith(Buffer.from([4, 3]));
240
+ expect(callback).not.toHaveBeenCalled();
241
+ expect(failCallback).toHaveBeenCalled();
240
242
  });
241
243
  });
242
244
 
243
- it('should emit fail on handlePairingFailed', () => {
244
- const callback = sinon.spy();
245
+ test('should emit fail on handlePairingFailed', () => {
246
+ const callback = jest.fn();
245
247
  smp.on('fail', callback);
246
248
  smp.handlePairingFailed();
247
- assert.calledOnceWithExactly(callback);
249
+ expect(callback).toHaveBeenCalled();
248
250
  });
249
251
 
250
- it('should emit ltk on handleEncryptInfo', () => {
251
- const callback = sinon.spy();
252
+ test('should emit ltk on handleEncryptInfo', () => {
253
+ const callback = jest.fn();
252
254
  smp.on('ltk', callback);
253
255
  smp.handleEncryptInfo(Buffer.from([0x02, 0x03, 0x04]));
254
- assert.calledOnceWithExactly(callback, Buffer.from([0x03, 0x04]));
256
+ expect(callback).toHaveBeenCalledWith(Buffer.from([0x03, 0x04]));
255
257
  });
256
258
 
257
- it('should emit ltk on handleMasterIdent', () => {
258
- const callback = sinon.spy();
259
+ test('should emit masterIdent on handleMasterIdent', () => {
260
+ const callback = jest.fn();
259
261
  smp.on('masterIdent', callback);
260
262
  smp.handleMasterIdent(Buffer.from([0x02, 0x03, 0x04, 0x05, 0x06]));
261
- assert.calledOnceWithExactly(callback, Buffer.from([0x03, 0x04]), Buffer.from([0x05, 0x06]));
263
+ expect(callback).toHaveBeenCalledWith(Buffer.from([0x03, 0x04]), Buffer.from([0x05, 0x06]));
262
264
  });
263
265
 
264
- it('should write on aclStream', () => {
266
+ test('should write on aclStream', () => {
265
267
  smp.write('data');
266
- assert.calledOnceWithExactly(aclStream.write, 6, 'data');
268
+ expect(aclStream.write).toHaveBeenCalledWith(6, 'data');
267
269
  });
268
- });
270
+ });