Files
peardata/test/protocol.test.js
T
Raven Scott 32928f19bd
CI / test (push) Successful in 1m8s
Release rolling / release (push) Successful in 8m25s
First Try: QVAC (QuantumVerse Automatic Computer)
2026-07-30 13:42:14 -04:00

106 lines
2.7 KiB
JavaScript

import test from 'brittle'
import {
Roles,
roleAllows,
MethodRoles,
PROTOCOL,
PROTOCOL_VERSION,
Methods,
Pushes,
} from '../shared/protocol.js'
import { validateMethodArgs, SCHEMA_VERSION } from '../shared/schema.js'
import { CHART_DEFS, CONTEXT_IDS, getAllChartDefs, getContextIds } from '../shared/metrics.js'
test('protocol constants', (t) => {
t.is(PROTOCOL, 'peardata/rpc')
t.ok(PROTOCOL_VERSION >= 1)
t.ok(SCHEMA_VERSION >= 1)
})
test('roleAllows hierarchy', (t) => {
t.ok(roleAllows(Roles.admin, Roles.viewer))
t.ok(roleAllows(Roles.operator, Roles.viewer))
t.ok(roleAllows(Roles.operator, Roles.operator))
t.absent(roleAllows(Roles.viewer, Roles.operator))
t.absent(roleAllows(Roles.viewer, Roles.admin))
})
test('method roles cover monitoring surface', (t) => {
for (const m of [
'handshake',
'ping',
'queryData',
'subscribeMetrics',
'listCharts',
'mintInvite',
'runJob',
'getDbInfo',
'linkPeer',
'unlinkPeer',
'getFleetHealth',
'listChildPeers',
'getWeights',
'queryLogs',
'listProcesses',
'getStorageInfo',
'getRetentionConfig',
'setRetentionConfig',
'pruneHistory',
'getHostSnapshot',
'searchCharts',
'summarizeChart',
]) {
t.ok(MethodRoles[m], m)
t.is(Methods[m], m)
}
})
test('pushes include metrics + anomaly', (t) => {
t.ok(Pushes.metrics.startsWith('push:'))
t.ok(Pushes.anomaly.startsWith('push:'))
})
test('metrics catalog covers system stats', (t) => {
t.ok(CHART_DEFS.length >= 30)
t.ok(getAllChartDefs().length >= CHART_DEFS.length)
const contexts = getContextIds()
for (const id of [
'system.cpu',
'system.ram',
'system.load',
'system.io',
'system.net',
'system.ip',
'mem.available',
'mem.swap',
'ip.tcpsock',
'ipv4.packets',
'system.cpu_some_pressure',
]) {
t.ok(contexts.includes(id), id)
}
t.ok(CONTEXT_IDS.includes('system.cpu'))
})
test('validateMethodArgs queryData', (t) => {
t.absent(validateMethodArgs('queryData', {}).ok)
t.ok(validateMethodArgs('queryData', { chart: 'system.cpu' }).ok)
t.absent(validateMethodArgs('queryData', { chart: 'system.cpu', points: 0 }).ok)
})
test('validateMethodArgs subscribeMetrics', (t) => {
const r = validateMethodArgs('subscribeMetrics', { charts: ['system.cpu'] })
t.ok(r.ok)
t.ok(r.args.intervalMs >= 500)
})
test('validateMethodArgs mintInvite', (t) => {
t.ok(validateMethodArgs('mintInvite', { role: 'operator' }).ok)
t.absent(validateMethodArgs('mintInvite', { role: 'god' }).ok)
})
test('validateMethodArgs revokePeer', (t) => {
t.absent(validateMethodArgs('revokePeer', { peerId: 'abc' }).ok)
t.ok(validateMethodArgs('revokePeer', { peerId: 'a'.repeat(64) }).ok)
})