This repository has been archived by the owner on Jun 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.js
156 lines (135 loc) · 3.13 KB
/
test.js
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
const test = require('ava')
const pino = require('pino')
const mock = require('mock-require')
const SerialPort = require('serialport/test')
const createPort = require('./app/server')
const MockBinding = SerialPort.Binding
const logger = pino({ level: 'silent' })
function getPort(t, dbClient, opts) {
const handleError = err => t.fail(err)
const port = createPort(SerialPort, dbClient, logger, handleError, opts)
return port
}
function createKnexMock(handleInsert) {
mock('knex', function() {
return {
insert(data) {
return {
into(table) {
handleInsert(table, data)
return Promise.resolve()
},
}
},
}
})
}
function getDbClient() {
const createDbClient = mock.reRequire('./app/db')
return createDbClient({
username: 'DB_USERNAME',
password: 'DB_PASSWORD',
host: 'DB_HOST',
port: 'DB_PORT',
name: 'DB_NAME',
})
}
test.afterEach.always(() => mock.stopAll())
test.cb.serial('enter shell mode and send lec command', t => {
t.plan(3)
const opts = {
shellCommandReceived() {
t.is(port.binding.lastWrite.toString('utf8'), '\r\r')
port.binding.emitData(Buffer.from('\r\ndwm> '))
},
lecCommandReceived() {
t.is(port.binding.lastWrite.toString('utf8'), 'lec\r')
port.binding.emitData(Buffer.from('lec\r\ndwm> '))
},
ready() {
t.pass()
t.end()
},
}
MockBinding.createPort('/dev/ttyACM0', {
echo: false,
record: true,
readyData: '',
})
const port = getPort(t, null, opts)
})
test.cb.serial('read coordinates from serial port', t => {
t.plan(2)
createKnexMock((table, data) => {
t.is(table, 'coordinates')
t.deepEqual(data, {
sensor_id: '8B32',
x: 1.74,
y: 0.38,
z: 0.42,
accuracy: 100,
hex: 'x16',
})
t.end()
})
const dbClient = getDbClient()
MockBinding.createPort('/dev/ttyACM0', {
echo: false,
record: true,
readyData: '',
})
const port = getPort(t, dbClient)
port.on('open', () => {
port.binding.emitData(Buffer.from('POS,0,8B32,1.74,0.38,0.42,100,x16\r\n'))
})
})
test.cb.serial('Can read multiple messages', t => {
t.plan(6)
const expectedCoordinates = [
{
sensor_id: '8B32',
x: 1.77,
y: 0.55,
z: 0.32,
accuracy: 100,
hex: 'x0C',
},
{
sensor_id: '8B32',
x: 1.76,
y: 0.56,
z: 0.33,
accuracy: 100,
hex: 'x0B',
},
{
sensor_id: '8B32',
x: 1.74,
y: 0.38,
z: 0.42,
accuracy: 100,
hex: 'x16',
},
]
createKnexMock((table, data) => {
t.is(table, 'coordinates')
t.deepEqual(data, expectedCoordinates.pop())
if (!expectedCoordinates.length) {
t.end()
}
})
const dbClient = getDbClient()
MockBinding.createPort('/dev/ttyACM0', {
echo: false,
record: true,
readyData: '',
})
const port = getPort(t, dbClient)
port.on('open', () => {
port.binding.emitData(
Buffer.from(
'dwm> POS,0,8B32,1.74,0.38,0.42,100,x16\r\nPOS,0,8B32,1.76,0.56,0.33,100,x0B\r\nPOS,0,8B32,1.77,0.55,0.32,100,x0C\r\n'
)
)
})
})