@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.
- package/README.md +393 -650
- package/examples/advertisement-discovery.js +57 -48
- package/examples/connect-address.js +59 -34
- package/examples/echo.js +59 -69
- package/examples/enter-exit.js +55 -49
- package/examples/multiple-bindings.js +53 -0
- package/examples/peripheral-explorer-async.js +39 -21
- package/examples/peripheral-explorer.ts +52 -0
- package/index.d.ts +249 -209
- package/index.js +4 -1
- package/jest.config.js +4 -0
- package/lib/characteristic.js +153 -127
- package/lib/{win/src/callbacks.h → common/include/Emit.h} +17 -14
- package/lib/common/include/Peripheral.h +31 -0
- package/lib/common/include/ThreadSafeCallback.h +95 -0
- package/lib/{win/src/callbacks.cc → common/src/Emit.cc} +111 -68
- package/lib/descriptor.js +57 -54
- package/lib/hci-socket/acl-stream.js +2 -4
- package/lib/hci-socket/bindings.js +96 -73
- package/lib/hci-socket/gap.js +2 -3
- package/lib/hci-socket/gatt.js +2 -5
- package/lib/hci-socket/hci.js +19 -7
- package/lib/hci-socket/signaling.js +2 -3
- package/lib/hci-socket/smp.js +2 -3
- package/lib/hci-socket/vs.js +1 -0
- package/lib/mac/binding.gyp +5 -7
- package/lib/mac/bindings.js +1 -3
- package/lib/mac/src/ble_manager.h +1 -8
- package/lib/mac/src/ble_manager.mm +87 -44
- package/lib/mac/src/napi_objc.h +1 -0
- package/lib/mac/src/napi_objc.mm +0 -6
- package/lib/mac/src/noble_mac.h +5 -3
- package/lib/mac/src/noble_mac.mm +99 -57
- package/lib/mac/src/objc_cpp.h +3 -2
- package/lib/mac/src/objc_cpp.mm +0 -6
- package/lib/noble.js +579 -488
- package/lib/peripheral.js +171 -174
- package/lib/resolve-bindings.js +37 -30
- package/lib/service.js +58 -55
- package/lib/win/binding.gyp +4 -11
- package/lib/win/bindings.js +1 -3
- package/lib/win/src/ble_manager.cc +291 -166
- package/lib/win/src/ble_manager.h +11 -13
- package/lib/win/src/napi_winrt.cc +1 -7
- package/lib/win/src/napi_winrt.h +1 -1
- package/lib/win/src/noble_winrt.cc +88 -61
- package/lib/win/src/noble_winrt.h +5 -3
- package/lib/win/src/notify_map.cc +0 -7
- package/lib/win/src/notify_map.h +1 -8
- package/lib/win/src/peripheral_winrt.cc +29 -11
- package/lib/win/src/peripheral_winrt.h +1 -1
- package/lib/win/src/radio_watcher.cc +79 -69
- package/lib/win/src/radio_watcher.h +30 -11
- package/lib/win/src/winrt_cpp.cc +1 -1
- package/lib/win/src/winrt_cpp.h +3 -0
- package/package.json +14 -17
- package/prebuilds/darwin-x64+arm64/@stoprocent+noble.node +0 -0
- package/prebuilds/win32-ia32/@stoprocent+noble.node +0 -0
- package/prebuilds/win32-x64/@stoprocent+noble.node +0 -0
- package/test/lib/characteristic.test.js +202 -322
- package/test/lib/descriptor.test.js +62 -95
- package/test/lib/hci-socket/acl-stream.test.js +112 -108
- package/test/lib/hci-socket/bindings.test.js +576 -365
- package/test/lib/hci-socket/hci.test.js +442 -473
- package/test/lib/hci-socket/signaling.test.js +45 -48
- package/test/lib/hci-socket/smp.test.js +144 -142
- package/test/lib/hci-socket/vs.test.js +193 -18
- package/test/lib/peripheral.test.js +492 -322
- package/test/lib/resolve-bindings.test.js +207 -82
- package/test/lib/service.test.js +79 -88
- package/test/noble.test.js +381 -1085
- package/.editorconfig +0 -11
- package/.nycrc.json +0 -4
- package/codecov.yml +0 -5
- package/examples/cache-gatt-discovery.js +0 -198
- package/examples/cache-gatt-reconnect.js +0 -164
- package/examples/ext-advertisement-discovery.js +0 -65
- package/examples/peripheral-explorer.js +0 -225
- package/examples/pizza/central.js +0 -194
- package/examples/pizza/pizza.js +0 -60
- package/examples/test/test.custom.js +0 -131
- package/examples/uart-bind-params.js +0 -28
- package/lib/distributed/bindings.js +0 -326
- package/lib/mac/src/callbacks.cc +0 -222
- package/lib/mac/src/callbacks.h +0 -84
- package/lib/mac/src/peripheral.h +0 -23
- package/lib/resolve-bindings-web.js +0 -9
- package/lib/webbluetooth/bindings.js +0 -368
- package/lib/websocket/bindings.js +0 -321
- package/lib/win/src/peripheral.h +0 -23
- package/test/lib/distributed/bindings.test.js +0 -918
- package/test/lib/webbluetooth/bindings.test.js +0 -190
- package/test/lib/websocket/bindings.test.js +0 -456
- package/test/mocha.setup.js +0 -0
- package/with-bindings.js +0 -5
- package/with-custom-binding.js +0 -6
- 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
|
-
|
|
30
|
-
|
|
27
|
+
afterEach(() => {
|
|
28
|
+
jest.clearAllMocks();
|
|
31
29
|
});
|
|
32
30
|
|
|
33
|
-
|
|
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
|
-
|
|
43
|
-
|
|
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
|
-
|
|
50
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
78
|
-
const callback =
|
|
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
|
-
|
|
86
|
-
|
|
87
|
-
|
|
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
|
-
|
|
90
|
+
test('should callback with error, data', () => {
|
|
96
91
|
const mockData = Buffer.alloc(0);
|
|
97
|
-
const callback =
|
|
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
|
-
|
|
105
|
-
|
|
106
|
-
|
|
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
|
-
|
|
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
|
-
|
|
128
|
-
|
|
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
|
-
|
|
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
|
-
|
|
146
|
-
|
|
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
|
-
|
|
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
|
-
|
|
147
|
+
expect(() => descriptor.writeValue(mockData)).toThrow(
|
|
172
148
|
'data must be a Buffer'
|
|
173
149
|
);
|
|
174
150
|
|
|
175
|
-
|
|
151
|
+
expect(mockNoble.writeValue).not.toHaveBeenCalled();
|
|
176
152
|
});
|
|
177
153
|
|
|
178
|
-
|
|
154
|
+
test('should delegate to noble', () => {
|
|
179
155
|
const mockData = Buffer.alloc(0);
|
|
180
156
|
descriptor.writeValue(mockData);
|
|
181
157
|
|
|
182
|
-
|
|
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
|
-
|
|
168
|
+
test('should callback', () => {
|
|
193
169
|
const mockData = Buffer.alloc(0);
|
|
194
|
-
const callback =
|
|
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
|
-
|
|
202
|
-
|
|
203
|
-
|
|
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
|
-
|
|
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
|
-
|
|
226
|
-
|
|
227
|
-
|
|
194
|
+
await expect(descriptor.writeValueAsync(mockData)).rejects.toThrow(
|
|
195
|
+
'data must be a Buffer'
|
|
196
|
+
);
|
|
228
197
|
});
|
|
229
198
|
|
|
230
|
-
|
|
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
|
-
|
|
236
|
-
|
|
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
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
-
|
|
10
|
-
|
|
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
|
-
|
|
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 =
|
|
24
|
-
const handle =
|
|
25
|
-
const localAddressType =
|
|
26
|
-
const localAddress =
|
|
27
|
-
const remoteAddressType =
|
|
28
|
-
const remoteAddress =
|
|
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
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
should(aclStream._smp).instanceOf(Smp);
|
|
30
|
+
expect(aclStream._hci).toBe(hci);
|
|
31
|
+
expect(aclStream._handle).toBe(handle);
|
|
36
32
|
|
|
37
|
-
|
|
38
|
-
|
|
33
|
+
expect(Smp).toHaveBeenCalledTimes(1);
|
|
34
|
+
expect(Smp).toHaveBeenCalledWith(aclStream, localAddressType, localAddress, remoteAddressType, remoteAddress);
|
|
39
35
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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 =
|
|
48
|
-
const handle =
|
|
49
|
-
const localAddressType =
|
|
50
|
-
const localAddress =
|
|
51
|
-
const remoteAddressType =
|
|
52
|
-
const remoteAddress =
|
|
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
|
-
|
|
54
|
+
expect(aclStream._smp.sendPairingRequest).toHaveBeenCalledTimes(1);
|
|
55
|
+
expect(aclStream._smp.sendPairingRequest).toHaveBeenCalledWith();
|
|
61
56
|
});
|
|
62
57
|
|
|
63
58
|
it('write', () => {
|
|
64
|
-
const hci =
|
|
65
|
-
const handle =
|
|
66
|
-
const localAddressType =
|
|
67
|
-
const localAddress =
|
|
68
|
-
const remoteAddressType =
|
|
69
|
-
const remoteAddress =
|
|
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
|
-
|
|
66
|
+
hci.writeAclDataPkt = jest.fn().mockResolvedValue(null);
|
|
72
67
|
|
|
73
|
-
aclStream
|
|
68
|
+
const aclStream = new AclStream(hci, handle, localAddressType, localAddress, remoteAddressType, remoteAddress);
|
|
74
69
|
|
|
75
70
|
aclStream.write('cid', 'data');
|
|
76
71
|
|
|
77
|
-
|
|
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 =
|
|
82
|
-
const handle =
|
|
83
|
-
const localAddressType =
|
|
84
|
-
const localAddress =
|
|
85
|
-
const remoteAddressType =
|
|
86
|
-
const remoteAddress =
|
|
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
|
|
91
|
-
aclStream.on('data',
|
|
86
|
+
const eventEmitted = jest.fn().mockResolvedValue(null);
|
|
87
|
+
aclStream.on('data', eventEmitted);
|
|
92
88
|
|
|
93
89
|
aclStream.push('cid', 'data');
|
|
94
90
|
|
|
95
|
-
|
|
91
|
+
expect(eventEmitted).toHaveBeenCalledTimes(1);
|
|
92
|
+
expect(eventEmitted).toHaveBeenCalledWith('cid', 'data');
|
|
96
93
|
});
|
|
97
94
|
|
|
98
95
|
it('push no data', () => {
|
|
99
|
-
const hci =
|
|
100
|
-
const handle =
|
|
101
|
-
const localAddressType =
|
|
102
|
-
const localAddress =
|
|
103
|
-
const remoteAddressType =
|
|
104
|
-
const remoteAddress =
|
|
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
|
|
109
|
-
aclStream.on('end',
|
|
105
|
+
const eventEmitted = jest.fn().mockResolvedValue(null);
|
|
106
|
+
aclStream.on('end', eventEmitted);
|
|
110
107
|
|
|
111
108
|
aclStream.push('cid');
|
|
112
109
|
|
|
113
|
-
|
|
110
|
+
expect(eventEmitted).toHaveBeenCalledTimes(1);
|
|
111
|
+
expect(eventEmitted).toHaveBeenCalledWith();
|
|
114
112
|
});
|
|
115
113
|
|
|
116
114
|
it('pushEncrypt', () => {
|
|
117
|
-
const hci =
|
|
118
|
-
const handle =
|
|
119
|
-
const localAddressType =
|
|
120
|
-
const localAddress =
|
|
121
|
-
const remoteAddressType =
|
|
122
|
-
const remoteAddress =
|
|
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
|
|
127
|
-
aclStream.on('encrypt',
|
|
124
|
+
const eventEmitted = jest.fn().mockResolvedValue(null);
|
|
125
|
+
aclStream.on('encrypt', eventEmitted);
|
|
128
126
|
|
|
129
127
|
aclStream.pushEncrypt('cid');
|
|
130
128
|
|
|
131
|
-
|
|
129
|
+
expect(eventEmitted).toHaveBeenCalledTimes(1);
|
|
130
|
+
expect(eventEmitted).toHaveBeenCalledWith('cid');
|
|
132
131
|
});
|
|
133
132
|
|
|
134
133
|
it('onSmpStk', () => {
|
|
135
|
-
const hci =
|
|
136
|
-
const handle =
|
|
137
|
-
const localAddressType =
|
|
138
|
-
const localAddress =
|
|
139
|
-
const remoteAddressType =
|
|
140
|
-
const remoteAddress =
|
|
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
|
-
|
|
141
|
+
hci.startLeEncryption = jest.fn().mockResolvedValue(null);
|
|
143
142
|
|
|
144
|
-
aclStream
|
|
143
|
+
const aclStream = new AclStream(hci, handle, localAddressType, localAddress, remoteAddressType, remoteAddress);
|
|
145
144
|
|
|
146
145
|
aclStream.onSmpStk('stk');
|
|
147
146
|
|
|
148
|
-
|
|
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 =
|
|
153
|
-
const handle =
|
|
154
|
-
const localAddressType =
|
|
155
|
-
const localAddress =
|
|
156
|
-
const remoteAddressType =
|
|
157
|
-
const remoteAddress =
|
|
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
|
|
162
|
-
aclStream.on('encryptFail',
|
|
166
|
+
const eventEmitted = jest.fn().mockResolvedValue(null);
|
|
167
|
+
aclStream.on('encryptFail', eventEmitted);
|
|
163
168
|
|
|
164
169
|
aclStream.onSmpFail();
|
|
165
170
|
|
|
166
|
-
|
|
171
|
+
expect(eventEmitted).toHaveBeenCalledTimes(1);
|
|
172
|
+
expect(eventEmitted).toHaveBeenCalledWith();
|
|
167
173
|
});
|
|
168
174
|
|
|
169
175
|
it('onSmpEnd', () => {
|
|
170
|
-
const hci =
|
|
171
|
-
const handle =
|
|
172
|
-
const localAddressType =
|
|
173
|
-
const localAddress =
|
|
174
|
-
const remoteAddressType =
|
|
175
|
-
const remoteAddress =
|
|
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
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
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
|
+
});
|