-
-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathbindings.test.ts
More file actions
376 lines (323 loc) · 12.5 KB
/
bindings.test.ts
File metadata and controls
376 lines (323 loc) · 12.5 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
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
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { assert, shouldReject } from '../test/assert'
import { makeTestFeature } from '../test/makeTestFeature'
import { BindingInterface, OpenOptions, PortInfo, SetOptions } from '@noelneu/bindings-interface'
import { autoDetect } from './index'
import { MockBinding } from '@serialport/binding-mock'
import { BindingsError } from './errors'
// All bindings are required to work with an "echo" firmware
const TEST_PORT = process.env.TEST_PORT
const TEST_BAUD = Number(process.env.TEST_BAUDRATE) || 115200
const defaultOpenOptions: OpenOptions = {
path: 'bad path',
baudRate: TEST_BAUD,
dataBits: 8,
hupcl: false,
lock: true,
parity: 'none',
rtscts: false,
stopBits: 1,
xany: false,
xoff: false,
xon: false,
}
const defaultSetFlags: SetOptions = {
brk: false,
cts: false,
dtr: true,
rts: true,
}
interface MockBinding extends BindingInterface {
createPort(path: string, options: any): void
}
const listFields = ['path', 'manufacturer', 'serialNumber', 'pnpId', 'locationId', 'vendorId', 'productId']
testBinding('mock', MockBinding, '/dev/exists')
testBinding(process.platform, autoDetect(), TEST_PORT)
function testBinding(bindingName: string, Binding: BindingInterface, testPort?: string) {
const { testFeature, describeHardware } = makeTestFeature(bindingName, testPort)
describe(`bindings/${bindingName}`, () => {
before(() => {
if (bindingName === 'mock' && testPort) {
(Binding as MockBinding).createPort(testPort, { echo: true })
}
})
describe('static method', () => {
describe('.list', () => {
it('returns an array', async () => {
const ports = await Binding.list()
assert.isArray(ports)
})
it('has objects with undefined when there is no data', async () => {
const ports = await Binding.list()
assert.isArray(ports)
if (ports.length === 0) {
console.log('no ports to test')
return
}
ports.forEach(port => {
assert.containSubset(Object.keys(port), listFields)
Object.keys(port).forEach((key: keyof PortInfo) => {
assert.notEqual(port[key], '', 'empty values should be undefined')
assert.isNotNull(port[key], 'empty values should be undefined')
})
})
})
})
describeHardware('.open', () => {
it('errors when providing a bad port', async () => {
const err = await shouldReject(Binding.open({
path: 'COMBAD',
baudRate: 115200,
}))
assert.include(err.message, 'COMBAD')
})
it('throws when not given a path', async () => {
await shouldReject(Binding.open({
path: '',
baudRate: 115200,
}), TypeError)
})
it('throws when not given a baudRate', async () => {
const err = await shouldReject(Binding.open({
path: 'COMBAD',
baudRate: undefined,
} as any))
assert.include(err.message, 'baudRate')
})
it('throws when not given options', async () => {
const err = await shouldReject(Binding.open(undefined as any))
assert.include(err.message, 'options')
})
describeHardware('with hardware', () => {
it('keeps open state', async () => {
const port = await Binding.open({ ...defaultOpenOptions, path: testPort! })
assert.equal(port.isOpen, true)
await port.close()
assert.equal(port.isOpen, false)
})
})
describeHardware('arbitrary baud rates', () => {
[25000, 1000000, 250000].forEach(testBaud => {
describe(`${testBaud} baud`, () => {
const customRates = { ...defaultOpenOptions, baudRate: testBaud, path: testPort! }
testFeature(`baudrate.${testBaud}`, `opens at ${testBaud} baud`, async () => {
const port = await Binding.open(customRates)
assert.equal(port.isOpen, true)
await port.close()
})
testFeature(`baudrate.${testBaud}_check`, `sets ${testBaud} baud successfully`, async () => {
const port = await Binding.open(customRates)
const { baudRate } = await port.getBaudRate()
assert.equal(baudRate, customRates.baudRate)
await port.close()
})
})
})
})
describeHardware('optional locking', () => {
const options = { ...defaultOpenOptions, path: testPort!, lock: true }
it('locks the port by default', async () => {
const port = await Binding.open(options)
assert.equal(port.isOpen, true)
await shouldReject(Binding.open(options))
await port.close()
})
testFeature('open.unlock', 'can unlock the port', async () => {
const noLock = { ...options, lock: false }
const port = await Binding.open(noLock)
assert.equal(port.isOpen, true)
const port2 = await Binding.open(noLock)
assert.equal(port2.isOpen, true)
await Promise.all([port.close(), port2.close()])
})
})
})
})
describeHardware('PortInstance', () => {
const options = { ...defaultOpenOptions, path: testPort! }
describe('#isOpen', () => {
it('is true after open and false after close', async () => {
const port = await Binding.open(options)
assert.equal(port.isOpen, true)
await port.close()
assert.equal(port.isOpen, false)
})
})
describe('#close', () => {
it('errors when already closed', async () => {
const port = await Binding.open(options)
assert.equal(port.isOpen, true)
await port.close()
assert.equal(port.isOpen, false)
await shouldReject(port.close())
})
it('closes an open port', async () => {
const port = await Binding.open(options)
await port.close()
assert.equal(port.isOpen, false)
})
})
describe('#update', () => {
it('errors when not given an object', async () => {
const port = await Binding.open(options)
await shouldReject(port.update(undefined as any), TypeError)
await port.close()
})
it('errors when updating nothing', async () => {
const port = await Binding.open(options)
await shouldReject(port.update({} as any), Error)
await port.close()
})
it('errors when closed', async () => {
const port = await Binding.open(options)
await port.close()
await shouldReject(port.update({ baudRate: 57600 }), Error)
})
testFeature('getBaudRate', 'updates baudRate', async () => {
const port = await Binding.open(options)
await port.update({ baudRate: 57600 })
const { baudRate } = await port.getBaudRate()
assert.equal(baudRate, 57600)
await port.close()
})
})
// because of the nature of opening and closing the ports a fair amount of data
// is left over on the pipe and isn't cleared when flushed on unix
describe('#read', () => {
const smallSampleData = Buffer.from('12345')
it('errors when closed', async () => {
const buffer = Buffer.alloc(5)
const port = await Binding.open(options)
await port.close()
await shouldReject(port.read(buffer, 0, buffer.length), Error)
})
it('reads requested number of bytes', async () => {
const buffer = Buffer.alloc(smallSampleData.length)
const port = await Binding.open(options)
await port.write(smallSampleData)
await port.read(buffer, 0, buffer.length)
assert.deepEqual(smallSampleData, buffer, 'Data matches')
await port.close()
})
it('returns at maximum the requested number of bytes and the buffer', async () => {
const buffer = Buffer.alloc(smallSampleData.length)
const port = await Binding.open(options)
await port.write(smallSampleData)
const { bytesRead, buffer: returnedBuffer } = await port.read(buffer, 0, 1)
assert.equal(bytesRead, 1)
assert.strictEqual(buffer, returnedBuffer)
assert.deepEqual(buffer, Buffer.from([smallSampleData[0], 0, 0, 0, 0]))
await port.close()
})
it('cancels the read when the port is closed', async () => {
const port = await Binding.open(options)
const readError = shouldReject(port.read(Buffer.alloc(100), 0, 100))
await port.close()
const err = await readError as BindingsError
assert.isTrue(err.canceled)
})
})
describe('#write', () => {
it('errors when not given a buffer', async () => {
const port = await Binding.open(options)
await shouldReject(port.write(null as any), TypeError)
await port.close()
})
it('errors when closed', async () => {
const data = Buffer.from('data!')
const port = await Binding.open(options)
await port.close()
await shouldReject(port.write(data), Error)
})
it('resolves after a small write', async () => {
const data = Buffer.from('simple write of 24 bytes')
const port = await Binding.open(options)
await port.write(data)
await port.close()
})
it('resolves after a large write (2k)', async function () {
this.timeout(20000)
const data = Buffer.alloc(1024 * 2)
const port = await Binding.open(options)
await port.write(data)
await port.close()
})
it('resolves after an empty write', async () => {
const data = Buffer.from([])
const port = await Binding.open(options)
await port.write(data)
await port.close()
})
})
describe('#drain', () => {
it('errors when closed', async () => {
const port = await Binding.open(options)
await port.close()
await shouldReject(port.drain(), Error)
})
it('drains the port', async () => {
const port = await Binding.open(options)
await port.drain()
await port.close()
})
it('waits for in progress writes to finish', async () => {
const port = await Binding.open(options)
let finishedWrite = false
const write = port.write(Buffer.alloc(1024 * 2)).then(() => {
finishedWrite = true
})
await port.drain()
assert.isTrue(finishedWrite)
await write
await port.close()
})
})
describe('#flush', () => {
it('errors when closed', async () => {
const port = await Binding.open(options)
await port.close()
await shouldReject(port.flush(), Error)
})
it('flushes the port', async () => {
const port = await Binding.open(options)
await port.flush()
await port.close()
})
})
describe('#set', () => {
it('errors when closed', async () => {
const port = await Binding.open(options)
await port.close()
await shouldReject(port.set(defaultSetFlags), Error)
})
it('throws when not called with options', async () => {
const port = await Binding.open(options)
await shouldReject(port.set(undefined as any), TypeError)
await port.close()
})
it('sets flags on the port', async () => {
const port = await Binding.open(options)
await port.set(defaultSetFlags)
await port.close()
})
})
describe('#get', () => {
it('errors when closed', async () => {
const port = await Binding.open(options)
await port.close()
await shouldReject(port.get(), Error)
})
it('gets modem line status from the port', async () => {
const port = await Binding.open(options)
const status = await port.get()
assert.isObject(status)
assert.isBoolean(status.cts)
assert.isBoolean(status.dsr)
assert.isBoolean(status.dcd)
await port.close()
})
})
})
})
}