-
-
Notifications
You must be signed in to change notification settings - Fork 203
Expand file tree
/
Copy pathhistory-api.ts
More file actions
244 lines (215 loc) · 7.73 KB
/
Copy pathhistory-api.ts
File metadata and controls
244 lines (215 loc) · 7.73 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
import chai from 'chai'
import fs from 'fs'
import path from 'path'
import { Value } from '@sinclair/typebox/value'
import { type TSchema, FormatRegistry } from '@sinclair/typebox'
import {
ValuesResponseSchema,
HistoryProvidersResponseSchema
} from '@signalk/server-api/typebox'
import { freeport } from './ts-servertestutilities'
import { startServerP } from './servertestutilities'
// eslint-disable-next-line @typescript-eslint/no-require-imports
const Server = require('../dist/')
chai.should()
FormatRegistry.Set('date-time', (value) => !isNaN(Date.parse(value)))
const FROM = '2025-01-01T00:00:00Z'
const TO = '2025-01-02T00:00:00Z'
function assertSchema(schema: TSchema, value: unknown, name: string) {
const valid = Value.Check(schema, value)
if (!valid) {
const errors = [...Value.Errors(schema, value)]
chai.assert.fail(
`${name} validation failed:\n${JSON.stringify(errors, null, 2)}`
)
}
}
function mkDirSync(dirPath: string) {
fs.mkdirSync(dirPath, { recursive: true })
}
describe('History API v2', () => {
describe('without provider', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let server: any
let api: string
let origConfigDir: string | undefined
before(async function () {
origConfigDir = process.env.SIGNALK_NODE_CONFIG_DIR
const port = await freeport()
api = `http://localhost:${port}/signalk/v2/api`
server = await startServerP(port, false)
})
after(async function () {
await server.stop()
if (origConfigDir === undefined) {
delete process.env.SIGNALK_NODE_CONFIG_DIR
} else {
process.env.SIGNALK_NODE_CONFIG_DIR = origConfigDir
}
})
it('returns 501 for /history/values when no provider is registered', async function () {
const res = await fetch(
`${api}/history/values?paths=navigation.position&from=${FROM}&to=${TO}`
)
res.status.should.equal(501)
const body = await res.json()
body.should.have.property('error')
})
})
describe('with provider', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let server: any
let api: string
let origConfigDir: string | undefined
let pluginConfigFile: string
before(async function () {
origConfigDir = process.env.SIGNALK_NODE_CONFIG_DIR
process.env.SIGNALK_NODE_CONFIG_DIR = path.join(
__dirname,
'plugin-test-config'
)
const pluginConfig = {
enabled: true,
configuration: {}
}
const configDir = path.join(
__dirname,
'plugin-test-config',
'plugin-config-data'
)
mkDirSync(configDir)
pluginConfigFile = path.join(configDir, 'testplugin.json')
fs.writeFileSync(pluginConfigFile, JSON.stringify(pluginConfig))
const port = await freeport()
api = `http://localhost:${port}/signalk/v2/api`
server = new Server({
config: { settings: { port } }
})
await server.start()
})
after(async function () {
await server.stop()
if (fs.existsSync(pluginConfigFile)) {
fs.unlinkSync(pluginConfigFile)
}
if (origConfigDir === undefined) {
delete process.env.SIGNALK_NODE_CONFIG_DIR
} else {
process.env.SIGNALK_NODE_CONFIG_DIR = origConfigDir
}
})
it('lists testplugin as default provider', async function () {
const res = await fetch(`${api}/history/_providers`)
res.status.should.equal(200)
const body = await res.json()
assertSchema(
HistoryProvidersResponseSchema,
body,
'HistoryProvidersResponse'
)
body.testplugin.isDefault.should.equal(true)
})
it('returns the default provider id', async function () {
const res = await fetch(`${api}/history/_providers/_default`)
res.status.should.equal(200)
const body = await res.json()
body.should.have.property('id', 'testplugin')
})
it('returns values from the provider', async function () {
const res = await fetch(
`${api}/history/values?paths=navigation.position&from=${FROM}&to=${TO}&resolution=60`
)
res.status.should.equal(200)
const body = await res.json()
assertSchema(ValuesResponseSchema, body, 'ValuesResponse')
body.data.length.should.be.greaterThan(0)
})
it('passes sourcePolicy=all to the provider', async function () {
const res = await fetch(
`${api}/history/values?paths=navigation.speedOverGround&from=${FROM}&to=${TO}&resolution=60&sourcePolicy=all`
)
res.status.should.equal(200)
const body = await res.json()
assertSchema(ValuesResponseSchema, body, 'ValuesResponse')
body.values.should.deep.equal([
{
path: 'navigation.speedOverGround',
method: 'average',
$source: 'source.a'
},
{
path: 'navigation.speedOverGround',
method: 'average',
$source: 'source.b'
}
])
body.data.should.deep.equal([['2025-01-01T12:00:00Z', 1.2, 2.4]])
})
it('returns 400 for invalid sourcePolicy', async function () {
const res = await fetch(
`${api}/history/values?paths=navigation.position&from=${FROM}&to=${TO}&sourcePolicy=unknown`
)
res.status.should.equal(400)
const body = await res.json()
body.error.should.contain('sourcePolicy')
})
it('returns paths from the provider', async function () {
const res = await fetch(`${api}/history/paths?from=${FROM}&to=${TO}`)
res.status.should.equal(200)
const body = await res.json()
body.should.be.an('array')
body.should.include('navigation.position')
})
it('returns contexts from the provider', async function () {
const res = await fetch(`${api}/history/contexts?from=${FROM}&to=${TO}`)
res.status.should.equal(200)
const body = await res.json()
body.should.be.an('array')
body.should.include('vessels.self')
})
it('returns 400 when paths is missing', async function () {
const res = await fetch(`${api}/history/values?from=${FROM}&to=${TO}`)
res.status.should.equal(400)
const body = await res.json()
body.should.have.property('error')
body.error.should.contain('paths')
})
it('accepts a time expression resolution like 1m', async function () {
const res = await fetch(
`${api}/history/values?paths=navigation.position&from=${FROM}&to=${TO}&resolution=1m`
)
res.status.should.equal(200)
})
it('returns 400 for unparseable resolution', async function () {
const res = await fetch(
`${api}/history/values?paths=navigation.position&from=${FROM}&to=${TO}&resolution=1y`
)
res.status.should.equal(400)
const body = await res.json()
body.error.should.contain('resolution')
})
it('accepts an ISO 8601 duration string', async function () {
const res = await fetch(
`${api}/history/values?paths=navigation.position&duration=PT15M`
)
res.status.should.equal(200)
})
it('accepts an integer number of seconds for duration', async function () {
const res = await fetch(
`${api}/history/values?paths=navigation.position&duration=900`
)
res.status.should.equal(200)
})
it('returns 400 for an unparseable duration', async function () {
const res = await fetch(
`${api}/history/values?paths=navigation.position&duration=not-a-duration`
)
res.status.should.equal(400)
const body = await res.json()
// Match against the specific error from the parser, not just any
// mention of "duration", to avoid false greens from unrelated
// validators that also mention the word.
body.error.should.contain('ISO 8601')
})
})
})