119 lines
3.2 KiB
JavaScript
119 lines
3.2 KiB
JavaScript
import test from 'brittle'
|
|
import { handleRest } from '../server/rest/routes.js'
|
|
import { getStore } from '../server/services/store.js'
|
|
import { initAuthKeys } from '../server/core/auth-keys.js'
|
|
import crypto from 'hypercore-crypto'
|
|
import b4a from 'b4a'
|
|
|
|
// REST routes read public key helper — init dummy keys
|
|
const seed = crypto.randomBytes(32)
|
|
initAuthKeys({
|
|
seedHex: b4a.toString(seed, 'hex'),
|
|
publicKeyHex: b4a.toString(crypto.keyPair(seed).publicKey, 'hex'),
|
|
})
|
|
|
|
test('REST /api/v3/info', async (t) => {
|
|
const res = await handleRest('/api/v3/info', new URLSearchParams())
|
|
t.is(res.status, 200)
|
|
t.ok(res.body.hostname)
|
|
t.ok(res.body.peardata)
|
|
})
|
|
|
|
test('REST /api/v1/charts after ingest', async (t) => {
|
|
getStore().ingest([
|
|
{
|
|
chart: 'system.load',
|
|
context: 'system.load',
|
|
ts: Date.now(),
|
|
values: { load1: 0.5, load5: 0.4, load15: 0.3 },
|
|
},
|
|
])
|
|
const res = await handleRest('/api/v1/charts', new URLSearchParams())
|
|
t.is(res.status, 200)
|
|
t.ok(res.body.charts['system.load'] || res.body.charts['system.cpu'])
|
|
})
|
|
|
|
test('REST /api/v3/data', async (t) => {
|
|
const now = Date.now()
|
|
for (let i = 0; i < 10; i++) {
|
|
getStore().ingest([
|
|
{
|
|
chart: 'system.cpu',
|
|
context: 'system.cpu',
|
|
ts: now - (10 - i) * 1000,
|
|
values: {
|
|
user: i,
|
|
system: 1,
|
|
nice: 0,
|
|
iowait: 0,
|
|
irq: 0,
|
|
softirq: 0,
|
|
idle: 99 - i,
|
|
},
|
|
},
|
|
])
|
|
}
|
|
const res = await handleRest(
|
|
'/api/v3/data',
|
|
new URLSearchParams({ chart: 'system.cpu', after: '-30', points: '10' })
|
|
)
|
|
t.is(res.status, 200)
|
|
t.ok(Array.isArray(res.body.data))
|
|
})
|
|
|
|
test('REST /api/v3/contexts', async (t) => {
|
|
const res = await handleRest('/api/v3/contexts', new URLSearchParams())
|
|
t.is(res.status, 200)
|
|
t.ok(res.body.contexts['system.cpu'])
|
|
})
|
|
|
|
test('REST /api/v3/db', async (t) => {
|
|
const res = await handleRest('/api/v3/db', new URLSearchParams())
|
|
t.is(res.status, 200)
|
|
t.ok('enabled' in res.body)
|
|
})
|
|
|
|
test('REST 404', async (t) => {
|
|
const res = await handleRest('/api/v9/nope', new URLSearchParams())
|
|
t.is(res.status, 404)
|
|
})
|
|
|
|
test('REST /api/v3/ai/snapshot', async (t) => {
|
|
const res = await handleRest('/api/v3/ai/snapshot', new URLSearchParams())
|
|
t.is(res.status, 200)
|
|
t.ok(res.body.hostname)
|
|
t.ok(res.body.kpis)
|
|
t.ok(res.body.catalog)
|
|
})
|
|
|
|
test('REST /api/v3/ai/charts', async (t) => {
|
|
const res = await handleRest(
|
|
'/api/v3/ai/charts',
|
|
new URLSearchParams({ q: 'cpu', limit: '10' })
|
|
)
|
|
t.is(res.status, 200)
|
|
t.ok(Array.isArray(res.body.results))
|
|
t.ok(res.body.results.some((r) => String(r.id).includes('cpu')))
|
|
})
|
|
|
|
test('REST /api/v3/ai/chart summary', async (t) => {
|
|
const now = Date.now()
|
|
for (let i = 0; i < 5; i++) {
|
|
getStore().ingest([
|
|
{
|
|
chart: 'system.load',
|
|
context: 'system.load',
|
|
ts: now - (5 - i) * 1000,
|
|
values: { load1: i * 0.1, load5: 0.2, load15: 0.1 },
|
|
},
|
|
])
|
|
}
|
|
const res = await handleRest(
|
|
'/api/v3/ai/chart/system.load/summary',
|
|
new URLSearchParams({ after: '-30', points: '10' })
|
|
)
|
|
t.is(res.status, 200)
|
|
t.is(res.body.chart, 'system.load')
|
|
t.ok(res.body.dims?.load1 || res.body.points >= 0)
|
|
})
|