Files
opencode-system-monitor/tests/metrics.test.mjs
T
David Meincke aa8a5f35f6 feat: support Linux system monitoring
Add procfs memory and mount-aware disk collectors, Intel and AMD GPU backends, bounded Intel sampling, portable validation tools, and Linux setup documentation. Bump to 0.2.0 and verify on Windows and Ubuntu 24.04 under WSL2.
2026-11-20 13:51:04 +01:00

74 lines
3.5 KiB
JavaScript

import { test } from "node:test"
import assert from "node:assert/strict"
import { Monitor, aggregateEngines, cpuUsage } from "../dist/metrics.js"
import { intelCountersScript } from "../dist/windows-gpu.js"
import { bytes, percent } from "../dist/format.js"
const intel = "luid_0x00000000_0x000242BD"
const nvidia = "luid_0x00000000_0x00024847"
const engine = (pid, luid, index, value) => ({
Name: `pid_${pid}_${luid}_phys_0_eng_${index}_engtype_3D`,
UtilizationPercentage: value,
})
test("Windows engine readings sum processes per engine, never different engines or adapters", () => {
assert.equal(aggregateEngines([
engine(1, intel, 0, 30), engine(2, intel, 0, 40),
engine(1, intel, 1, 55), engine(3, nvidia, 0, 60),
]), 70)
assert.equal(aggregateEngines([engine(1, intel, 0, 80), engine(2, intel, 0, 40)]), 100)
assert.equal(aggregateEngines([]), null)
assert.equal(aggregateEngines([{ Name: "invalid", UtilizationPercentage: 99 }]), null)
assert.equal(aggregateEngines([engine(1, intel, 0, 0)]), 0)
})
test("CPU counters handle normal deltas, idle, missing intervals, and counter resets", () => {
assert.equal(cpuUsage({ idle: 100, total: 200 }, { idle: 150, total: 400 }), 75)
assert.equal(cpuUsage({ idle: 100, total: 200 }, { idle: 200, total: 300 }), 0)
assert.equal(cpuUsage({ idle: 100, total: 200 }, { idle: 100, total: 200 }), null)
assert.equal(cpuUsage({ idle: 100, total: 200 }, { idle: 10, total: 20 }), null)
})
test("Only a DXGI LUID can be inserted in the fixed PowerShell query", () => {
assert.match(intelCountersScript(intel), /luid_0x00000000_0x000242BD/)
assert.throws(() => intelCountersScript("'; exit; '"), /Invalid GPU adapter identity/)
})
test("Windows Intel-only collection excludes another GPU, coalesces concurrent calls, and caches", async () => {
let calls = 0
const collector = new Monitor(async (file, args) => {
calls++
assert.match(file, /powershell\.exe$/)
const script = Buffer.from(args.at(-1), "base64").toString("utf16le")
if (script.includes("Add-Type")) return JSON.stringify({ name: "Intel(R) Graphics", luid: intel, sharedLimit: 16 * 1024 ** 3 })
if (script.includes("Win32_LogicalDisk")) return JSON.stringify([{ DeviceID: "C:", VolumeName: "OS", Size: 1000, FreeSpace: 750 }])
return JSON.stringify({
engines: [engine(1, intel, 0, 12), engine(2, intel, 0, 8), engine(3, nvidia, 0, 99)],
memory: [{ SharedUsage: 256 * 1024 ** 2 }],
})
}, { platform: "win32" })
const first = collector.snapshot()
assert.equal(first, collector.snapshot())
const snapshot = await first
assert.equal(snapshot.gpu.adapter.percent, 20)
assert.equal(snapshot.gpu.adapter.memoryUsed, 256 * 1024 ** 2)
assert.equal(snapshot.gpu.adapter.memoryKind, "shared")
assert.equal(snapshot.disks.items[0].percent, 25)
assert.equal(snapshot.gpu.error, null)
assert.equal(await collector.snapshot(), snapshot)
assert.equal(calls, 3)
})
test("A missing GPU or failed Windows disk query shows unavailable, never false idle/full values", async () => {
const collector = new Monitor(async () => { throw new Error("Permission denied") }, { platform: "win32" })
const snapshot = await collector.snapshot()
assert.equal(snapshot.gpu.adapter, null)
assert.match(snapshot.gpu.error, /Permission denied/)
assert.equal(snapshot.disks.items.length, 0)
assert.match(snapshot.disks.error, /Permission denied/)
assert.ok(snapshot.ram.total > 0)
assert.equal(percent(null), "—")
assert.equal(bytes(null), "Unavailable")
assert.match(bytes(1024 ** 3), /1 GiB/)
})