@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,8 +1,3 @@
1
- const sinon = require('sinon');
2
- const should = require('should');
3
-
4
- const { assert } = sinon;
5
-
6
1
  const Descriptor = require('../../lib/descriptor');
7
2
 
8
3
  describe('descriptor', () => {
@@ -15,7 +10,10 @@ describe('descriptor', () => {
15
10
  let descriptor = null;
16
11
 
17
12
  beforeEach(() => {
18
- mockNoble = {};
13
+ mockNoble = {
14
+ readValue: jest.fn(),
15
+ writeValue: jest.fn()
16
+ };
19
17
 
20
18
  descriptor = new Descriptor(
21
19
  mockNoble,
@@ -26,11 +24,15 @@ describe('descriptor', () => {
26
24
  );
27
25
  });
28
26
 
29
- it('should have a uuid', () => {
30
- should(descriptor.uuid).equal(mockUuid);
27
+ afterEach(() => {
28
+ jest.clearAllMocks();
31
29
  });
32
30
 
33
- it('should lookup name and type by uuid', () => {
31
+ test('should have a uuid', () => {
32
+ expect(descriptor.uuid).toEqual(mockUuid);
33
+ });
34
+
35
+ test('should lookup name and type by uuid', () => {
34
36
  descriptor = new Descriptor(
35
37
  mockNoble,
36
38
  mockPeripheralId,
@@ -39,211 +41,176 @@ describe('descriptor', () => {
39
41
  '2900'
40
42
  );
41
43
 
42
- should(descriptor.name).equal('Characteristic Extended Properties');
43
- should(descriptor.type).equal(
44
+ expect(descriptor.name).toEqual('Characteristic Extended Properties');
45
+ expect(descriptor.type).toEqual(
44
46
  'org.bluetooth.descriptor.gatt.characteristic_extended_properties'
45
47
  );
46
48
  });
47
49
 
48
50
  describe('toString', () => {
49
- it('should be uuid, name, type', () => {
50
- should(descriptor.toString()).equal(
51
+ test('should be uuid, name, type', () => {
52
+ expect(descriptor.toString()).toEqual(
51
53
  '{"uuid":"mock-uuid","name":null,"type":null}'
52
54
  );
53
55
  });
54
56
  });
55
57
 
56
58
  describe('readValue', () => {
57
- beforeEach(() => {
58
- mockNoble.readValue = sinon.spy();
59
- });
60
-
61
- afterEach(() => {
62
- sinon.reset();
63
- });
64
-
65
- it('should delegate to noble', () => {
59
+ test('should delegate to noble', () => {
66
60
  descriptor.readValue();
67
61
 
68
- assert.calledOnceWithExactly(
69
- mockNoble.readValue,
62
+ expect(mockNoble.readValue).toHaveBeenCalledWith(
70
63
  mockPeripheralId,
71
64
  mockServiceUuid,
72
65
  mockCharacteristicUuid,
73
66
  mockUuid
74
67
  );
68
+ expect(mockNoble.readValue).toHaveBeenCalledTimes(1);
75
69
  });
76
70
 
77
- it('should callback', () => {
78
- const callback = sinon.spy();
71
+ test('should callback', () => {
72
+ const callback = jest.fn();
79
73
 
80
74
  descriptor.readValue(callback);
81
75
  descriptor.emit('valueRead');
82
76
  // Check for single callback
83
77
  descriptor.emit('valueRead');
84
78
 
85
- assert.calledOnceWithExactly(callback, null, undefined);
86
- assert.calledOnceWithExactly(
87
- mockNoble.readValue,
79
+ expect(callback).toHaveBeenCalledWith(undefined, undefined);
80
+ expect(callback).toHaveBeenCalledTimes(1);
81
+ expect(mockNoble.readValue).toHaveBeenCalledWith(
88
82
  mockPeripheralId,
89
83
  mockServiceUuid,
90
84
  mockCharacteristicUuid,
91
85
  mockUuid
92
86
  );
87
+ expect(mockNoble.readValue).toHaveBeenCalledTimes(1);
93
88
  });
94
89
 
95
- it('should callback with error, data', () => {
90
+ test('should callback with error, data', () => {
96
91
  const mockData = Buffer.alloc(0);
97
- const callback = sinon.spy();
92
+ const callback = jest.fn();
98
93
 
99
94
  descriptor.readValue(callback);
100
95
  descriptor.emit('valueRead', mockData);
101
96
  // Check for single callback
102
97
  descriptor.emit('valueRead', mockData);
103
98
 
104
- assert.calledOnceWithExactly(callback, null, mockData);
105
- assert.calledOnceWithExactly(
106
- mockNoble.readValue,
99
+ expect(callback).toHaveBeenCalledWith(undefined, mockData);
100
+ expect(callback).toHaveBeenCalledTimes(1);
101
+ expect(mockNoble.readValue).toHaveBeenCalledWith(
107
102
  mockPeripheralId,
108
103
  mockServiceUuid,
109
104
  mockCharacteristicUuid,
110
105
  mockUuid
111
106
  );
107
+ expect(mockNoble.readValue).toHaveBeenCalledTimes(1);
112
108
  });
113
109
  });
114
110
 
115
111
  describe('readValueAsync', () => {
116
- beforeEach(() => {
117
- mockNoble.readValue = sinon.spy();
118
- });
119
-
120
- afterEach(() => {
121
- sinon.reset();
122
- });
123
-
124
- it('should delegate to noble', async () => {
112
+ test('should delegate to noble', async () => {
125
113
  const promise = descriptor.readValueAsync();
126
114
  descriptor.emit('valueRead');
127
- // Check for single callback
128
- descriptor.emit('valueRead');
129
-
130
- should(promise).resolvedWith(undefined);
131
- assert.calledOnceWithExactly(
132
- mockNoble.readValue,
115
+
116
+ await expect(promise).resolves.toBeUndefined();
117
+ expect(mockNoble.readValue).toHaveBeenCalledWith(
133
118
  mockPeripheralId,
134
119
  mockServiceUuid,
135
120
  mockCharacteristicUuid,
136
121
  mockUuid
137
122
  );
123
+ expect(mockNoble.readValue).toHaveBeenCalledTimes(1);
138
124
  });
139
125
 
140
- it('should resolve with data', async () => {
126
+ test('should resolve with data', async () => {
141
127
  const mockData = Buffer.alloc(0);
142
128
 
143
129
  const promise = descriptor.readValueAsync();
144
130
  descriptor.emit('valueRead', mockData);
145
- // Check for single callback
146
- descriptor.emit('valueRead', mockData);
147
-
148
- should(promise).resolvedWith(mockData);
149
- assert.calledOnceWithExactly(
150
- mockNoble.readValue,
131
+
132
+ await expect(promise).resolves.toEqual(mockData);
133
+ expect(mockNoble.readValue).toHaveBeenCalledWith(
151
134
  mockPeripheralId,
152
135
  mockServiceUuid,
153
136
  mockCharacteristicUuid,
154
137
  mockUuid
155
138
  );
139
+ expect(mockNoble.readValue).toHaveBeenCalledTimes(1);
156
140
  });
157
141
  });
158
142
 
159
143
  describe('writeValue', () => {
160
- beforeEach(() => {
161
- mockNoble.writeValue = sinon.spy();
162
- });
163
-
164
- afterEach(() => {
165
- sinon.reset();
166
- });
167
-
168
- it('should only accept data as a buffer', () => {
144
+ test('should only accept data as a buffer', () => {
169
145
  const mockData = {};
170
146
 
171
- should(() => descriptor.writeValue(mockData)).throw(
147
+ expect(() => descriptor.writeValue(mockData)).toThrow(
172
148
  'data must be a Buffer'
173
149
  );
174
150
 
175
- assert.notCalled(mockNoble.writeValue);
151
+ expect(mockNoble.writeValue).not.toHaveBeenCalled();
176
152
  });
177
153
 
178
- it('should delegate to noble', () => {
154
+ test('should delegate to noble', () => {
179
155
  const mockData = Buffer.alloc(0);
180
156
  descriptor.writeValue(mockData);
181
157
 
182
- assert.calledOnceWithExactly(
183
- mockNoble.writeValue,
158
+ expect(mockNoble.writeValue).toHaveBeenCalledWith(
184
159
  mockPeripheralId,
185
160
  mockServiceUuid,
186
161
  mockCharacteristicUuid,
187
162
  mockUuid,
188
163
  mockData
189
164
  );
165
+ expect(mockNoble.writeValue).toHaveBeenCalledTimes(1);
190
166
  });
191
167
 
192
- it('should callback', () => {
168
+ test('should callback', () => {
193
169
  const mockData = Buffer.alloc(0);
194
- const callback = sinon.spy();
170
+ const callback = jest.fn();
195
171
 
196
172
  descriptor.writeValue(mockData, callback);
197
173
  descriptor.emit('valueWrite');
198
174
  // Check for single callback
199
175
  descriptor.emit('valueWrite');
200
176
 
201
- assert.calledOnceWithExactly(callback, null);
202
- assert.calledOnceWithExactly(
203
- mockNoble.writeValue,
177
+ expect(callback).toHaveBeenCalledWith(undefined);
178
+ expect(callback).toHaveBeenCalledTimes(1);
179
+ expect(mockNoble.writeValue).toHaveBeenCalledWith(
204
180
  mockPeripheralId,
205
181
  mockServiceUuid,
206
182
  mockCharacteristicUuid,
207
183
  mockUuid,
208
184
  mockData
209
185
  );
186
+ expect(mockNoble.writeValue).toHaveBeenCalledTimes(1);
210
187
  });
211
188
  });
212
189
 
213
190
  describe('writeValueAsync', () => {
214
- beforeEach(() => {
215
- mockNoble.writeValue = sinon.spy();
216
- });
217
-
218
- afterEach(() => {
219
- sinon.reset();
220
- });
221
-
222
- it('should only accept data as a buffer', async () => {
191
+ test('should only accept data as a buffer', async () => {
223
192
  const mockData = {};
224
193
 
225
- const promise = descriptor.writeValueAsync(mockData);
226
-
227
- should(promise).rejectedWith('data must be a Buffer');
194
+ await expect(descriptor.writeValueAsync(mockData)).rejects.toThrow(
195
+ 'data must be a Buffer'
196
+ );
228
197
  });
229
198
 
230
- it('should delegate to noble', async () => {
199
+ test('should delegate to noble', async () => {
231
200
  const mockData = Buffer.alloc(0);
232
201
 
233
202
  const promise = descriptor.writeValueAsync(mockData);
234
203
  descriptor.emit('valueWrite');
235
- // Check for single callback
236
- descriptor.emit('valueWrite');
237
-
238
- should(promise).resolvedWith(undefined);
239
- assert.calledOnceWithExactly(
240
- mockNoble.writeValue,
204
+
205
+ await expect(promise).resolves.toBeUndefined();
206
+ expect(mockNoble.writeValue).toHaveBeenCalledWith(
241
207
  mockPeripheralId,
242
208
  mockServiceUuid,
243
209
  mockCharacteristicUuid,
244
210
  mockUuid,
245
211
  mockData
246
212
  );
213
+ expect(mockNoble.writeValue).toHaveBeenCalledTimes(1);
247
214
  });
248
215
  });
249
- });
216
+ });
@@ -1,188 +1,192 @@
1
- const proxyquire = require('proxyquire').noCallThru();
2
- const should = require('should');
3
- const sinon = require('sinon');
4
- const { assert, fake, match } = sinon;
5
-
6
- describe('hci-socket acl-stream', () => {
7
- const Smp = sinon.stub();
1
+ const { jest: jestFn } = require('@jest/globals');
2
+
3
+ // Use jest.mock instead of proxyquire
4
+ jest.mock('../../../lib/hci-socket/smp', () => {
5
+ return jest.fn().mockImplementation(() => ({
6
+ on: jest.fn().mockResolvedValue(null),
7
+ removeListener: jest.fn().mockResolvedValue(null),
8
+ sendPairingRequest: jest.fn().mockResolvedValue(null)
9
+ }));
10
+ });
8
11
 
9
- const AclStream = proxyquire('../../../lib/hci-socket/acl-stream', {
10
- './smp': Smp
11
- });
12
+ const Smp = require('../../../lib/hci-socket/smp');
13
+ const AclStream = require('../../../lib/hci-socket/acl-stream');
12
14
 
15
+ describe('hci-socket acl-stream', () => {
13
16
  beforeEach(() => {
14
- Smp.prototype.on = fake.resolves(null);
15
- Smp.prototype.removeListener = fake.resolves(null);
16
- });
17
-
18
- afterEach(() => {
19
- sinon.reset();
17
+ jest.clearAllMocks();
20
18
  });
21
19
 
22
20
  it('constructor', () => {
23
- const hci = fake.resolves();
24
- const handle = fake.resolves();
25
- const localAddressType = fake.resolves();
26
- const localAddress = fake.resolves();
27
- const remoteAddressType = fake.resolves();
28
- const remoteAddress = fake.resolves();
21
+ const hci = jest.fn().mockResolvedValue();
22
+ const handle = jest.fn().mockResolvedValue();
23
+ const localAddressType = jest.fn().mockResolvedValue();
24
+ const localAddress = jest.fn().mockResolvedValue();
25
+ const remoteAddressType = jest.fn().mockResolvedValue();
26
+ const remoteAddress = jest.fn().mockResolvedValue();
29
27
 
30
28
  const aclStream = new AclStream(hci, handle, localAddressType, localAddress, remoteAddressType, remoteAddress);
31
29
 
32
- should(aclStream._hci).eql(hci);
33
- should(aclStream._handle).eql(handle);
34
-
35
- should(aclStream._smp).instanceOf(Smp);
30
+ expect(aclStream._hci).toBe(hci);
31
+ expect(aclStream._handle).toBe(handle);
36
32
 
37
- assert.calledOnce(Smp);
38
- assert.calledWith(Smp, aclStream, localAddressType, localAddress, remoteAddressType, remoteAddress);
33
+ expect(Smp).toHaveBeenCalledTimes(1);
34
+ expect(Smp).toHaveBeenCalledWith(aclStream, localAddressType, localAddress, remoteAddressType, remoteAddress);
39
35
 
40
- assert.calledThrice(aclStream._smp.on);
41
- assert.calledWith(aclStream._smp.on, 'stk', match.any);
42
- assert.calledWith(aclStream._smp.on, 'fail', match.any);
43
- assert.calledWith(aclStream._smp.on, 'end', match.any);
36
+ expect(aclStream._smp.on).toHaveBeenCalledTimes(3);
37
+ expect(aclStream._smp.on).toHaveBeenCalledWith('stk', expect.any(Function));
38
+ expect(aclStream._smp.on).toHaveBeenCalledWith('fail', expect.any(Function));
39
+ expect(aclStream._smp.on).toHaveBeenCalledWith('end', expect.any(Function));
44
40
  });
45
41
 
46
42
  it('encrypt', () => {
47
- const hci = fake.resolves();
48
- const handle = fake.resolves();
49
- const localAddressType = fake.resolves();
50
- const localAddress = fake.resolves();
51
- const remoteAddressType = fake.resolves();
52
- const remoteAddress = fake.resolves();
43
+ const hci = jest.fn().mockResolvedValue();
44
+ const handle = jest.fn().mockResolvedValue();
45
+ const localAddressType = jest.fn().mockResolvedValue();
46
+ const localAddress = jest.fn().mockResolvedValue();
47
+ const remoteAddressType = jest.fn().mockResolvedValue();
48
+ const remoteAddress = jest.fn().mockResolvedValue();
53
49
 
54
50
  const aclStream = new AclStream(hci, handle, localAddressType, localAddress, remoteAddressType, remoteAddress);
55
51
 
56
- aclStream._smp.sendPairingRequest = fake.resolves(null);
57
-
58
52
  aclStream.encrypt();
59
53
 
60
- assert.calledOnceWithExactly(aclStream._smp.sendPairingRequest);
54
+ expect(aclStream._smp.sendPairingRequest).toHaveBeenCalledTimes(1);
55
+ expect(aclStream._smp.sendPairingRequest).toHaveBeenCalledWith();
61
56
  });
62
57
 
63
58
  it('write', () => {
64
- const hci = fake.resolves();
65
- const handle = fake.resolves();
66
- const localAddressType = fake.resolves();
67
- const localAddress = fake.resolves();
68
- const remoteAddressType = fake.resolves();
69
- const remoteAddress = fake.resolves();
59
+ const hci = jest.fn().mockResolvedValue();
60
+ const handle = jest.fn().mockResolvedValue();
61
+ const localAddressType = jest.fn().mockResolvedValue();
62
+ const localAddress = jest.fn().mockResolvedValue();
63
+ const remoteAddressType = jest.fn().mockResolvedValue();
64
+ const remoteAddress = jest.fn().mockResolvedValue();
70
65
 
71
- const aclStream = new AclStream(hci, handle, localAddressType, localAddress, remoteAddressType, remoteAddress);
66
+ hci.writeAclDataPkt = jest.fn().mockResolvedValue(null);
72
67
 
73
- aclStream._hci.writeAclDataPkt = fake.resolves(null);
68
+ const aclStream = new AclStream(hci, handle, localAddressType, localAddress, remoteAddressType, remoteAddress);
74
69
 
75
70
  aclStream.write('cid', 'data');
76
71
 
77
- assert.calledOnceWithExactly(aclStream._hci.writeAclDataPkt, handle, 'cid', 'data');
72
+ expect(hci.writeAclDataPkt).toHaveBeenCalledTimes(1);
73
+ expect(hci.writeAclDataPkt).toHaveBeenCalledWith(handle, 'cid', 'data');
78
74
  });
79
75
 
80
76
  it('push data', () => {
81
- const hci = fake.resolves();
82
- const handle = fake.resolves();
83
- const localAddressType = fake.resolves();
84
- const localAddress = fake.resolves();
85
- const remoteAddressType = fake.resolves();
86
- const remoteAddress = fake.resolves();
77
+ const hci = jest.fn().mockResolvedValue();
78
+ const handle = jest.fn().mockResolvedValue();
79
+ const localAddressType = jest.fn().mockResolvedValue();
80
+ const localAddress = jest.fn().mockResolvedValue();
81
+ const remoteAddressType = jest.fn().mockResolvedValue();
82
+ const remoteAddress = jest.fn().mockResolvedValue();
87
83
 
88
84
  const aclStream = new AclStream(hci, handle, localAddressType, localAddress, remoteAddressType, remoteAddress);
89
85
 
90
- const eventEmmitted = fake.resolves(null);
91
- aclStream.on('data', eventEmmitted);
86
+ const eventEmitted = jest.fn().mockResolvedValue(null);
87
+ aclStream.on('data', eventEmitted);
92
88
 
93
89
  aclStream.push('cid', 'data');
94
90
 
95
- assert.calledOnceWithExactly(eventEmmitted, 'cid', 'data');
91
+ expect(eventEmitted).toHaveBeenCalledTimes(1);
92
+ expect(eventEmitted).toHaveBeenCalledWith('cid', 'data');
96
93
  });
97
94
 
98
95
  it('push no data', () => {
99
- const hci = fake.resolves();
100
- const handle = fake.resolves();
101
- const localAddressType = fake.resolves();
102
- const localAddress = fake.resolves();
103
- const remoteAddressType = fake.resolves();
104
- const remoteAddress = fake.resolves();
96
+ const hci = jest.fn().mockResolvedValue();
97
+ const handle = jest.fn().mockResolvedValue();
98
+ const localAddressType = jest.fn().mockResolvedValue();
99
+ const localAddress = jest.fn().mockResolvedValue();
100
+ const remoteAddressType = jest.fn().mockResolvedValue();
101
+ const remoteAddress = jest.fn().mockResolvedValue();
105
102
 
106
103
  const aclStream = new AclStream(hci, handle, localAddressType, localAddress, remoteAddressType, remoteAddress);
107
104
 
108
- const eventEmmitted = fake.resolves(null);
109
- aclStream.on('end', eventEmmitted);
105
+ const eventEmitted = jest.fn().mockResolvedValue(null);
106
+ aclStream.on('end', eventEmitted);
110
107
 
111
108
  aclStream.push('cid');
112
109
 
113
- assert.calledOnceWithExactly(eventEmmitted);
110
+ expect(eventEmitted).toHaveBeenCalledTimes(1);
111
+ expect(eventEmitted).toHaveBeenCalledWith();
114
112
  });
115
113
 
116
114
  it('pushEncrypt', () => {
117
- const hci = fake.resolves();
118
- const handle = fake.resolves();
119
- const localAddressType = fake.resolves();
120
- const localAddress = fake.resolves();
121
- const remoteAddressType = fake.resolves();
122
- const remoteAddress = fake.resolves();
115
+ const hci = jest.fn().mockResolvedValue();
116
+ const handle = jest.fn().mockResolvedValue();
117
+ const localAddressType = jest.fn().mockResolvedValue();
118
+ const localAddress = jest.fn().mockResolvedValue();
119
+ const remoteAddressType = jest.fn().mockResolvedValue();
120
+ const remoteAddress = jest.fn().mockResolvedValue();
123
121
 
124
122
  const aclStream = new AclStream(hci, handle, localAddressType, localAddress, remoteAddressType, remoteAddress);
125
123
 
126
- const eventEmmitted = fake.resolves(null);
127
- aclStream.on('encrypt', eventEmmitted);
124
+ const eventEmitted = jest.fn().mockResolvedValue(null);
125
+ aclStream.on('encrypt', eventEmitted);
128
126
 
129
127
  aclStream.pushEncrypt('cid');
130
128
 
131
- assert.calledOnceWithExactly(eventEmmitted, 'cid');
129
+ expect(eventEmitted).toHaveBeenCalledTimes(1);
130
+ expect(eventEmitted).toHaveBeenCalledWith('cid');
132
131
  });
133
132
 
134
133
  it('onSmpStk', () => {
135
- const hci = fake.resolves();
136
- const handle = fake.resolves();
137
- const localAddressType = fake.resolves();
138
- const localAddress = fake.resolves();
139
- const remoteAddressType = fake.resolves();
140
- const remoteAddress = fake.resolves();
134
+ const hci = jest.fn().mockResolvedValue();
135
+ const handle = jest.fn().mockResolvedValue();
136
+ const localAddressType = jest.fn().mockResolvedValue();
137
+ const localAddress = jest.fn().mockResolvedValue();
138
+ const remoteAddressType = jest.fn().mockResolvedValue();
139
+ const remoteAddress = jest.fn().mockResolvedValue();
141
140
 
142
- const aclStream = new AclStream(hci, handle, localAddressType, localAddress, remoteAddressType, remoteAddress);
141
+ hci.startLeEncryption = jest.fn().mockResolvedValue(null);
143
142
 
144
- aclStream._hci.startLeEncryption = fake.resolves(null);
143
+ const aclStream = new AclStream(hci, handle, localAddressType, localAddress, remoteAddressType, remoteAddress);
145
144
 
146
145
  aclStream.onSmpStk('stk');
147
146
 
148
- assert.calledOnceWithExactly(aclStream._hci.startLeEncryption, handle, Buffer.from('0000000000000000', 'hex'), Buffer.from('0000', 'hex'), 'stk');
147
+ expect(hci.startLeEncryption).toHaveBeenCalledTimes(1);
148
+ expect(hci.startLeEncryption).toHaveBeenCalledWith(
149
+ handle,
150
+ Buffer.from('0000000000000000', 'hex'),
151
+ Buffer.from('0000', 'hex'),
152
+ 'stk'
153
+ );
149
154
  });
150
155
 
151
156
  it('onSmpFail', () => {
152
- const hci = fake.resolves();
153
- const handle = fake.resolves();
154
- const localAddressType = fake.resolves();
155
- const localAddress = fake.resolves();
156
- const remoteAddressType = fake.resolves();
157
- const remoteAddress = fake.resolves();
157
+ const hci = jest.fn().mockResolvedValue();
158
+ const handle = jest.fn().mockResolvedValue();
159
+ const localAddressType = jest.fn().mockResolvedValue();
160
+ const localAddress = jest.fn().mockResolvedValue();
161
+ const remoteAddressType = jest.fn().mockResolvedValue();
162
+ const remoteAddress = jest.fn().mockResolvedValue();
158
163
 
159
164
  const aclStream = new AclStream(hci, handle, localAddressType, localAddress, remoteAddressType, remoteAddress);
160
165
 
161
- const eventEmmitted = fake.resolves(null);
162
- aclStream.on('encryptFail', eventEmmitted);
166
+ const eventEmitted = jest.fn().mockResolvedValue(null);
167
+ aclStream.on('encryptFail', eventEmitted);
163
168
 
164
169
  aclStream.onSmpFail();
165
170
 
166
- assert.calledOnceWithExactly(eventEmmitted);
171
+ expect(eventEmitted).toHaveBeenCalledTimes(1);
172
+ expect(eventEmitted).toHaveBeenCalledWith();
167
173
  });
168
174
 
169
175
  it('onSmpEnd', () => {
170
- const hci = fake.resolves();
171
- const handle = fake.resolves();
172
- const localAddressType = fake.resolves();
173
- const localAddress = fake.resolves();
174
- const remoteAddressType = fake.resolves();
175
- const remoteAddress = fake.resolves();
176
+ const hci = jest.fn().mockResolvedValue();
177
+ const handle = jest.fn().mockResolvedValue();
178
+ const localAddressType = jest.fn().mockResolvedValue();
179
+ const localAddress = jest.fn().mockResolvedValue();
180
+ const remoteAddressType = jest.fn().mockResolvedValue();
181
+ const remoteAddress = jest.fn().mockResolvedValue();
176
182
 
177
183
  const aclStream = new AclStream(hci, handle, localAddressType, localAddress, remoteAddressType, remoteAddress);
178
184
 
179
- aclStream._smp.sendPairingRequest = fake.resolves(null);
180
-
181
185
  aclStream.onSmpEnd();
182
186
 
183
- assert.calledThrice(aclStream._smp.removeListener);
184
- assert.calledWith(aclStream._smp.removeListener, 'stk', match.any);
185
- assert.calledWith(aclStream._smp.removeListener, 'fail', match.any);
186
- assert.calledWith(aclStream._smp.removeListener, 'end', match.any);
187
+ expect(aclStream._smp.removeListener).toHaveBeenCalledTimes(3);
188
+ expect(aclStream._smp.removeListener).toHaveBeenCalledWith('stk', expect.any(Function));
189
+ expect(aclStream._smp.removeListener).toHaveBeenCalledWith('fail', expect.any(Function));
190
+ expect(aclStream._smp.removeListener).toHaveBeenCalledWith('end', expect.any(Function));
187
191
  });
188
- });
192
+ });