-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest-comprehensive-signalk.js
More file actions
executable file
·187 lines (169 loc) · 4.65 KB
/
test-comprehensive-signalk.js
File metadata and controls
executable file
·187 lines (169 loc) · 4.65 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
#!/usr/bin/env node
/**
* Comprehensive test for the SignalK transformation endpoint
*/
const http = require('http')
// Test data - various NMEA 2000 messages that should convert to SignalK
const testCases = [
{
name: 'Wind Data (PGN 130306)',
data: {
timestamp: '2024-08-07T15:00:00.000Z',
prio: 2,
src: 42,
dst: 255,
pgn: 130306,
description: 'Wind Data',
fields: {
windSpeed: 10.5,
windAngle: 45.2,
reference: 'Apparent',
},
},
},
{
name: 'Rate of Turn (PGN 127251)',
data: {
timestamp: '2024-08-07T15:00:01.000Z',
prio: 2,
src: 17,
dst: 255,
pgn: 127251,
description: 'Rate of Turn',
fields: {
rate: 0.05236,
},
},
},
{
name: 'Vessel Heading (PGN 127250)',
data: {
timestamp: '2024-08-07T15:00:02.000Z',
prio: 2,
src: 17,
dst: 255,
pgn: 127250,
description: 'Vessel Heading',
fields: {
heading: 1.5708,
reference: 'Magnetic',
},
},
},
{
name: 'Multiple messages in array',
data: [
{
timestamp: '2024-08-07T15:00:03.000Z',
pgn: 127488,
src: 35,
fields: {
engineInstance: 0,
engineSpeed: 1800,
},
},
{
timestamp: '2024-08-07T15:00:04.000Z',
pgn: 127505,
src: 35,
fields: {
instance: 0,
fluidType: 'Fuel',
level: 75.5,
},
},
],
},
]
async function runTest(testCase, serverPort = 8081) {
return new Promise((resolve, reject) => {
const postData = JSON.stringify(testCase)
const options = {
hostname: 'localhost',
port: serverPort,
path: '/api/transform/signalk',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(postData),
},
}
const req = http.request(options, (res) => {
let data = ''
res.on('data', (chunk) => {
data += chunk
})
res.on('end', () => {
try {
const response = JSON.parse(data)
resolve({
status: res.statusCode,
response,
})
} catch (error) {
resolve({
status: res.statusCode,
rawResponse: data,
parseError: error.message,
})
}
})
})
req.on('error', (e) => {
reject(e)
})
req.write(postData)
req.end()
})
}
async function runAllTests() {
console.log('🧪 Testing SignalK Transformation Endpoint')
console.log('==========================================\n')
for (let i = 0; i < testCases.length; i++) {
const testCase = testCases[i]
console.log(`${i + 1}. Testing: ${testCase.name}`)
console.log('-'.repeat(50))
try {
const result = await runTest(testCase)
if (result.status === 200 && result.response.success) {
console.log('✅ SUCCESS')
console.log(` Messages processed: ${result.response.messagesProcessed}`)
console.log(` SignalK deltas generated: ${result.response.signalKDeltas?.length || 0}`)
if (result.response.signalKDeltas && result.response.signalKDeltas.length > 0) {
const firstDelta = result.response.signalKDeltas[0]
if (firstDelta.updates && firstDelta.updates[0] && firstDelta.updates[0].values) {
console.log(' Sample paths generated:')
firstDelta.updates[0].values.slice(0, 3).forEach((value) => {
console.log(` - ${value.path}: ${value.value}`)
})
if (firstDelta.updates[0].values.length > 3) {
console.log(` - ... and ${firstDelta.updates[0].values.length - 3} more`)
}
}
}
if (result.response.errors && result.response.errors.length > 0) {
console.log(` ⚠️ Errors: ${result.response.errors.length}`)
}
} else {
console.log('❌ FAILED')
console.log(` Status: ${result.status}`)
console.log(` Response: ${JSON.stringify(result.response || result.rawResponse, null, 2)}`)
}
} catch (error) {
console.log('❌ ERROR')
console.log(` ${error.message}`)
if (error.code === 'ECONNREFUSED') {
console.log(' Make sure Visual Analyzer server is running on port 8081')
console.log(' Run: npm run server')
break
}
}
console.log('')
}
console.log('Testing completed! 🎉')
console.log('')
console.log('To start the Visual Analyzer server:')
console.log(' npm run build:server && npm run server')
}
// Run the tests
runAllTests()