Files
bare-operating-system/packages/bare-os-coreutils/test/baretop-fixture.test.mjs
T
2026-04-05 17:07:57 -04:00

118 lines
4.0 KiB
JavaScript

import test from 'brittle'
import { readFile } from 'node:fs/promises'
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { createContext, runInContext } from 'node:vm'
const __dirname = dirname(fileURLToPath(import.meta.url))
const fixturePath = join(__dirname, 'fixtures/baretop-sample-metrics.json')
const processTableFixture = join(__dirname, 'fixtures/baretop-process-table.json')
const helpersPath = join(__dirname, '../lib/baretop-ui-helpers.js')
const snapPath = join(__dirname, '../lib/baretop-snapshot.js')
test('fixture metrics_live-shaped JSON parses with expected keys', async (t) => {
const raw = await readFile(fixturePath, 'utf8')
const o = JSON.parse(raw)
t.ok(o && typeof o.session === 'object')
t.is(Number(o.schema), 4)
t.is(Number(o.session.execLineCount), 42)
t.ok(typeof o.delegateInflight === 'object')
t.ok(o.replicationLive && typeof o.replicationLive === 'object')
t.ok(o.kernelCounters && typeof o.kernelCounters === 'object')
t.ok(
o.netSummaryNestedExample &&
typeof o.netSummaryNestedExample.replicationQueue === 'object'
)
})
test('baretop-snapshot preamble lists batch proc entries', async (t) => {
const src = await readFile(snapPath, 'utf8')
t.ok(src.includes('BARE_TOP_SNAPSHOT_PROC_ENTRIES'))
t.ok(src.includes("'/proc/bare_os/index.json'"))
t.ok(src.includes('bareOsReadBareTopSnapshot'))
t.ok(src.includes('bareTopHealthScore'))
t.ok(src.includes('BARE_TOP_SNAPSHOT_LITE_ENTRIES'))
t.ok(src.includes('bootBudgetSummary'))
t.ok(src.includes('security_posture.json'))
})
test('bareTopHealthScore penalizes stall and inflight cardinality', async (t) => {
const src = await readFile(snapPath, 'utf8')
const ctx = createContext({})
runInContext(src, ctx)
const m = {
delegateInflight: { a: 2, b: 3 },
replicationLive: { stallHint: 'yes' }
}
const s = ctx.bareTopHealthScore(m, 3)
t.ok(s < 80)
const good = {
peers: 2,
delegateInflight: {},
replicationLive: { peerCount: 2, stallHint: '' }
}
t.is(ctx.bareTopHealthScore(good, 2), 100)
})
test('bareTopHealthScore: no_peers + idle delegates is not stuck at 73', async (t) => {
const src = await readFile(snapPath, 'utf8')
const ctx = createContext({})
runInContext(src, ctx)
const idle = {
peers: 0,
delegateInflight: {},
replicationLive: { peerCount: 0, stallHint: 'no_peers' }
}
t.is(ctx.bareTopHealthScore(idle, 0), 100)
const lightInflight = {
peers: 0,
delegateInflight: { http: 1, dns: 0 },
replicationLive: { peerCount: 0, stallHint: 'no_peers' }
}
const s2 = ctx.bareTopHealthScore(lightInflight, 0)
t.ok(s2 >= 90)
t.not(s2, 73)
})
test('bareTopHealthScoreDetail: benign stall hints and replicationStall flag', async (t) => {
const src = await readFile(snapPath, 'utf8')
const ctx = createContext({})
runInContext(src, ctx)
const lenOk = {
peers: 0,
replicationLive: { stallHint: 'length_unavailable', peerCount: 0 }
}
t.is(ctx.bareTopHealthScore(lenOk, 0), 100)
const explicit = {
peers: 1,
replicationLive: { stallHint: 'ok', peerCount: 1, replicationStall: true }
}
t.is(ctx.bareTopHealthScore(explicit, 1), 80)
const d = ctx.bareTopHealthScoreDetail(explicit, 1, null)
t.ok(String(d.breakdown).includes('replication_stall_flag'))
})
test('process_table fixture maps to sorted PID rows via helpers', async (t) => {
const raw = await readFile(processTableFixture, 'utf8')
const pt = JSON.parse(raw)
const code = await readFile(helpersPath, 'utf8')
const ctx = createContext({})
runInContext(code, ctx)
const rows = ctx.bareTopProcessRowsFromTable(pt)
t.is(rows.length, 2)
const sorted = ctx.bareTopSortProcessRows(rows, 'pid', true)
t.is(ctx.bareTopProcessPid(sorted[0]), 2)
const line = ctx.bareTopFormatFixedColumns(
[
String(sorted[1].pid),
String(sorted[1].ppid),
String(sorted[1].name)
],
[4, 4, 20],
['r', 'r', 'l'],
80
)
t.ok(line.includes('3'))
t.ok(line.toLowerCase().includes('shell'))
})