Add a Desktop dashboard and text status command for disk space, RAM, CPU and Intel integrated graphics. Include visibility-driven sampling, tests, development tooling, and installation documentation.
163 lines
5.1 KiB
JavaScript
163 lines
5.1 KiB
JavaScript
import { test } from "node:test"
|
|
import assert from "node:assert/strict"
|
|
import { createRoot, createSignal } from "solid-js"
|
|
import { createMonitorClient } from "../dist/client.js"
|
|
|
|
const flush = () => new Promise(resolve => setImmediate(resolve))
|
|
|
|
function harness(t, customLoad) {
|
|
let time = 1_800_000_000_000
|
|
let sequence = 0
|
|
let calls = 0
|
|
const timers = new Map()
|
|
const addTimer = (callback, ms, repeat) => {
|
|
const id = ++sequence
|
|
timers.set(id, { callback, at: time + ms, repeat: repeat ? ms : 0 })
|
|
return id
|
|
}
|
|
t.mock.method(globalThis, "setTimeout", (callback, ms) => addTimer(callback, ms, false))
|
|
t.mock.method(globalThis, "setInterval", (callback, ms) => addTimer(callback, ms, true))
|
|
t.mock.method(globalThis, "clearTimeout", id => timers.delete(id))
|
|
t.mock.method(globalThis, "clearInterval", id => timers.delete(id))
|
|
t.mock.method(Date, "now", () => time)
|
|
const original = globalThis.document
|
|
const page = new EventTarget()
|
|
page.visibilityState = "visible"
|
|
globalThis.document = page
|
|
const sample = () => ({
|
|
sampledAt: time, hostname: "test", platform: "win32", uptime: 1,
|
|
cpu: { name: "CPU", cores: 1, percent: 10, perCore: [10] },
|
|
ram: { total: 100, used: 50, free: 50, percent: 50 },
|
|
disks: { sampledAt: time, items: [], error: null },
|
|
gpu: { sampledAt: time, adapter: null, error: null },
|
|
})
|
|
const state = createRoot(dispose => {
|
|
const [visible, setVisible] = createSignal(false)
|
|
const [interval, setInterval] = createSignal(3000)
|
|
const client = createMonitorClient(() => {
|
|
calls++
|
|
return customLoad ? customLoad(sample) : Promise.resolve(sample())
|
|
}, interval, visible)
|
|
return { client, setVisible, setInterval, dispose }
|
|
})
|
|
t.after(() => { state.dispose(); globalThis.document = original })
|
|
return {
|
|
...state, timers, sample, calls: () => calls,
|
|
windowVisible(value) {
|
|
page.visibilityState = value ? "visible" : "hidden"
|
|
page.dispatchEvent(new Event("visibilitychange"))
|
|
},
|
|
async advance(ms) {
|
|
const target = time + ms
|
|
for (;;) {
|
|
const next = [...timers].filter(([, timer]) => timer.at <= target).sort((a, b) => a[1].at - b[1].at)[0]
|
|
if (!next) break
|
|
const [id, timer] = next
|
|
time = timer.at
|
|
if (timer.repeat) timer.at += timer.repeat
|
|
else timers.delete(id)
|
|
timer.callback()
|
|
await flush()
|
|
}
|
|
time = target
|
|
await flush()
|
|
},
|
|
}
|
|
}
|
|
|
|
test("No requests or timers until a dashboard opens; closing it stops all polling", async t => {
|
|
const app = harness(t)
|
|
await flush()
|
|
assert.equal(app.calls(), 0)
|
|
assert.equal(app.timers.size, 0)
|
|
await app.advance(60_000)
|
|
await app.client.refresh()
|
|
assert.equal(app.calls(), 0)
|
|
|
|
app.setVisible(true)
|
|
await flush()
|
|
assert.equal(app.calls(), 1)
|
|
assert.equal(app.client.active(), true)
|
|
await app.advance(3000)
|
|
assert.equal(app.calls(), 2)
|
|
app.setVisible(false)
|
|
assert.equal(app.client.active(), false)
|
|
assert.equal(app.timers.size, 0)
|
|
const cached = app.client.snapshot()
|
|
await app.advance(120_000)
|
|
assert.equal(app.calls(), 2)
|
|
assert.equal(app.client.snapshot(), cached)
|
|
|
|
app.setVisible(true)
|
|
await flush()
|
|
assert.equal(app.calls(), 3)
|
|
})
|
|
|
|
test("Minimizing the window removes timers and showing it resumes only with an open dashboard", async t => {
|
|
const app = harness(t)
|
|
app.setVisible(true)
|
|
await flush()
|
|
app.windowVisible(false)
|
|
assert.equal(app.timers.size, 0)
|
|
await app.advance(30_000)
|
|
assert.equal(app.calls(), 1)
|
|
app.windowVisible(true)
|
|
await flush()
|
|
assert.equal(app.calls(), 2)
|
|
app.setVisible(false)
|
|
app.windowVisible(false)
|
|
app.windowVisible(true)
|
|
await app.advance(30_000)
|
|
assert.equal(app.calls(), 2)
|
|
assert.equal(app.timers.size, 0)
|
|
})
|
|
|
|
test("A request finishing after closure cannot restart the polling loop", async t => {
|
|
let resolve
|
|
const app = harness(t, () => new Promise(done => { resolve = done }))
|
|
app.setVisible(true)
|
|
await flush()
|
|
assert.equal(app.calls(), 1)
|
|
app.setVisible(false)
|
|
resolve(app.sample())
|
|
await flush()
|
|
assert.equal(app.timers.size, 0)
|
|
await app.advance(60_000)
|
|
assert.equal(app.calls(), 1)
|
|
})
|
|
|
|
test("Pause, refresh-rate changes, manual refresh, and cleanup respect visibility", async t => {
|
|
const app = harness(t)
|
|
app.setVisible(true)
|
|
await flush()
|
|
app.client.setPaused(true)
|
|
const before = app.calls()
|
|
await app.advance(30_000)
|
|
assert.equal(app.calls(), before)
|
|
await app.client.refresh()
|
|
assert.equal(app.calls(), before + 1)
|
|
app.setInterval(10_000)
|
|
app.client.setPaused(false)
|
|
await flush()
|
|
const resumed = app.calls()
|
|
await app.advance(9000)
|
|
assert.equal(app.calls(), resumed)
|
|
await app.advance(1000)
|
|
assert.equal(app.calls(), resumed + 1)
|
|
app.dispose()
|
|
assert.equal(app.timers.size, 0)
|
|
app.windowVisible(false)
|
|
app.windowVisible(true)
|
|
await app.advance(60_000)
|
|
assert.equal(app.calls(), resumed + 1)
|
|
})
|
|
|
|
test("Closing immediately after opening cancels a queued but not started request", async t => {
|
|
const app = harness(t)
|
|
app.setVisible(true)
|
|
app.setVisible(false)
|
|
await flush()
|
|
assert.equal(app.calls(), 0)
|
|
assert.equal(app.timers.size, 0)
|
|
})
|