-
-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathDevice.spec.js
More file actions
137 lines (103 loc) · 4.39 KB
/
Copy pathDevice.spec.js
File metadata and controls
137 lines (103 loc) · 4.39 KB
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
/* global test, expect, jest */
jest.doMock('../src/BusHelper', () => {
const EventEmitter = jest.requireActual('events')
return class BusHelperMock extends EventEmitter {
constructor () {
super()
this._prepare = jest.fn()
this.props = jest.fn()
this.prop = jest.fn()
this.set = jest.fn()
this.waitPropChange = jest.fn()
this.children = jest.fn()
this.callMethod = jest.fn()
}
}
})
jest.mock('../src/GattServer')
const dbus = Symbol('dbus')
const { Variant } = require('dbus-next')
const Device = require('../src/Device')
const GattServer = require('../src/GattServer')
test('props', async () => {
const device = new Device(dbus, 'hci0', 'dev_00_00_00_00_00_00')
device.helper.prop.mockImplementation((value) => Promise.resolve(({
Name: '_name_',
Address: '00:00:00:00:00:00',
AddressType: 'public',
Alias: '_alias_',
RSSI: 100,
TxPower: 50,
ManufacturerData: { 1: new Variant('ay', Buffer.from([0x01, 0x02])) },
AdvertisingData: { 2: new Variant('ay', Buffer.from([0x03, 0x04])) },
ServiceData: { 3: new Variant('ay', Buffer.from([0x05, 0x6])) },
Paired: true,
Connected: true
}[value])))
await expect(device.getName()).resolves.toEqual('_name_')
await expect(device.getAddress()).resolves.toEqual('00:00:00:00:00:00')
await expect(device.getAddressType()).resolves.toEqual('public')
await expect(device.getAlias()).resolves.toEqual('_alias_')
await expect(device.getRSSI()).resolves.toEqual(100)
await expect(device.getTXPower()).resolves.toEqual(50)
await expect(device.getManufacturerData()).resolves.toMatchObject({ 1: Buffer.from([0x01, 0x02]) })
await expect(device.getAdvertisingData()).resolves.toMatchObject({ 2: Buffer.from([0x03, 0x04]) })
await expect(device.getServiceData()).resolves.toMatchObject({ 3: Buffer.from([0x05, 0x06]) })
await expect(device.isConnected()).resolves.toEqual(true)
await expect(device.isPaired()).resolves.toEqual(true)
await expect(device.toString()).resolves.toEqual('_name_ [00:00:00:00:00:00]')
})
test('pairing', async () => {
const device = new Device(dbus, 'hci0', 'dev_00_00_00_00_00_00')
await expect(device.pair()).resolves.toBeUndefined()
expect(device.helper.callMethod).toHaveBeenLastCalledWith('Pair')
await expect(device.cancelPair()).resolves.toBeUndefined()
expect(device.helper.callMethod).toHaveBeenLastCalledWith('CancelPair')
expect(device.helper.callMethod).toHaveBeenCalledTimes(2)
})
test('connection', async () => {
const device = new Device(dbus, 'hci0', 'dev_00_00_00_00_00_00')
await expect(device.connect()).resolves.toBeUndefined()
expect(device.helper.callMethod).toHaveBeenLastCalledWith('Connect')
await expect(device.disconnect()).resolves.toBeUndefined()
expect(device.helper.callMethod).toHaveBeenLastCalledWith('Disconnect')
expect(device.helper.callMethod).toHaveBeenCalledTimes(2)
})
test('gatt server initialization', async () => {
const device = new Device(dbus, 'hci0', 'dev_00_00_00_00_00_00')
const gattServer = await device.gatt()
expect(GattServer).toHaveBeenCalledWith(dbus, 'hci0', 'dev_00_00_00_00_00_00')
expect(gattServer.init).toHaveBeenCalledTimes(1)
})
test('event:valuechanged', async () => {
const device = new Device(dbus, 'hci0', 'dev_00_00_00_00_00_00')
const connectedFn = jest.fn()
const disconnectedFn = jest.fn()
await device.connect()
device.on('connect', connectedFn)
device.on('disconnect', disconnectedFn)
device.helper.emit('PropertiesChanged',
{ Connected: { signature: 'b', value: true } }
)
expect(connectedFn).toHaveBeenCalledWith({ connected: true })
device.helper.emit('PropertiesChanged',
{ Connected: { signature: 'b', value: false } }
)
expect(disconnectedFn).toHaveBeenCalledWith({ connected: false })
await device.disconnect()
})
test('race condition in Device.js / disconnect()', async () => {
const device = new Device(dbus, 'hci0', 'dev_00_00_00_00_00_00')
await device.connect()
const disconnectedFn = jest.fn()
device.on('disconnect', disconnectedFn)
await device.disconnect()
// Send the disconnect event slightly after the call to disconnect()
device.helper.emit('PropertiesChanged',
{ Connected: { signature: 'b', value: false } }
)
// Check that the disconnect event has been received
expect(disconnectedFn).toHaveBeenCalledWith({ connected: false })
// Cleanup
device.off('disconnect', disconnectedFn)
})