-
-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathhistory-api.ts
More file actions
133 lines (113 loc) · 3.84 KB
/
history-api.ts
File metadata and controls
133 lines (113 loc) · 3.84 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
import chai from 'chai'
import fs from 'fs'
import path from 'path'
import { Value } from '@sinclair/typebox/value'
import { 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 mkDirSync(dirPath: string) {
if (!fs.existsSync(dirPath)) {
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
before(async function () {
const port = await freeport()
api = `http://localhost:${port}/signalk/v2/api`
server = await startServerP(port, false)
})
after(async function () {
await server.stop()
})
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
before(async function () {
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)
fs.writeFileSync(
path.join(configDir, 'testplugin.json'),
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()
})
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()
Value.Check(HistoryProvidersResponseSchema, body).should.equal(true)
body.testplugin.isDefault.should.equal(true)
})
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()
Value.Check(ValuesResponseSchema, body).should.equal(true)
body.data.length.should.be.greaterThan(0)
})
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)
})
})
})