-
-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathmdns-ws.test.ts
More file actions
212 lines (187 loc) · 5.3 KB
/
mdns-ws.test.ts
File metadata and controls
212 lines (187 loc) · 5.3 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
import { expect } from 'chai'
import MdnsWs from './mdns-ws'
import { createMockApp, createDebugStub } from './test-helpers'
const SK_HELLO = JSON.stringify({
name: 'test-server',
version: '2.0.0',
roles: ['master'],
self: 'vessels.urn:mrn:imo:mmsi:123456789'
})
const { WebSocketServer } = require('ws')
const { Writable } = require('stream')
function createSkWsServer(): Promise<{
wss: InstanceType<typeof WebSocketServer>
port: number
close: () => Promise<void>
}> {
return new Promise((resolve) => {
const wss = new WebSocketServer(
{
port: 0,
host: '127.0.0.1',
path: '/signalk/v1/stream'
},
() => {
const addr = wss.address() as { port: number }
resolve({
wss,
port: addr.port,
close: () =>
new Promise<void>((res) => {
wss.clients.forEach((c: { close: () => void }) => c.close())
wss.close(() => res())
})
})
}
)
wss.on('connection', (ws: { send: (data: string) => void }) => {
ws.send(SK_HELLO)
})
})
}
function createSink(): {
chunks: unknown[]
writable: InstanceType<typeof Writable>
} {
const chunks: unknown[] = []
const writable = new Writable({
objectMode: true,
write(chunk: unknown, _encoding: string, callback: () => void) {
chunks.push(chunk)
writable.emit('data-received', chunk)
callback()
}
})
return { chunks, writable }
}
describe('MdnsWs', () => {
it('sets provider status on successful connection', function (done) {
this.timeout(10000)
let server: Awaited<ReturnType<typeof createSkWsServer>>
createSkWsServer().then((s) => {
server = s
const app = createMockApp()
const mdns = new MdnsWs({
app,
providerId: 'test-mdns',
host: '127.0.0.1',
port: server.port,
createDebug: createDebugStub()
})
mdns.pipe(createSink().writable)
const check = setInterval(() => {
if (app.providerStatuses.some((s) => s.msg.includes('connected'))) {
clearInterval(check)
mdns.destroy()
server.close().then(() => done())
}
}, 100)
})
})
it('sets provider error on connection failure', function (done) {
this.timeout(10000)
const app = createMockApp()
const mdns = new MdnsWs({
app,
providerId: 'test-mdns',
host: '127.0.0.1',
port: 1,
createDebug: createDebugStub()
})
mdns.pipe(createSink().writable)
const check = setInterval(() => {
if (app.providerErrors.length > 0) {
clearInterval(check)
expect(app.providerErrors[0]!.id).to.equal('test-mdns')
mdns.destroy()
done()
}
}, 100)
})
it('detects disconnect when server closes', function (done) {
this.timeout(10000)
let server: Awaited<ReturnType<typeof createSkWsServer>>
createSkWsServer().then((s) => {
server = s
const app = createMockApp()
const mdns = new MdnsWs({
app,
providerId: 'test-mdns',
host: '127.0.0.1',
port: server.port,
createDebug: createDebugStub()
})
mdns.pipe(createSink().writable)
const checkConnected = setInterval(() => {
if (app.providerStatuses.some((s) => s.msg.includes('connected'))) {
clearInterval(checkConnected)
server.close().then(() => {
const checkDisconnect = setInterval(() => {
if (
app.providerErrors.some(
(e) => e.msg.includes('disconnect') || e.msg !== ''
)
) {
clearInterval(checkDisconnect)
mdns.destroy()
done()
}
}, 100)
})
}
}, 100)
})
})
it('receives delta data through the stream', function (done) {
this.timeout(10000)
let server: Awaited<ReturnType<typeof createSkWsServer>>
createSkWsServer().then((s) => {
server = s
server.wss.on('connection', (ws: { send: (data: string) => void }) => {
setTimeout(() => {
ws.send(
JSON.stringify({
context: 'vessels.urn:mrn:imo:mmsi:123456789',
updates: [
{
values: [{ path: 'navigation.speedOverGround', value: 3.5 }],
source: { label: 'test' }
}
]
})
)
}, 200)
})
const app = createMockApp()
const mdns = new MdnsWs({
app,
providerId: 'test-mdns',
host: '127.0.0.1',
port: server.port,
createDebug: createDebugStub()
})
const { chunks, writable } = createSink()
mdns.pipe(writable)
interface DeltaChunk {
updates?: Array<{
values?: Array<{ path: string }>
$source?: string
}>
}
writable.on('data-received', () => {
const delta = chunks.find(
(c) =>
(c as DeltaChunk)?.updates?.[0]?.values?.[0]?.path ===
'navigation.speedOverGround'
)
if (delta) {
expect((delta as DeltaChunk).updates![0]!['$source']).to.include(
'test-mdns'
)
mdns.destroy()
server.close().then(() => done())
}
})
})
})
})