Repository Public
1import { describe, expect, it } from "vitest";2import { clusterSeries, sanitizeNode, summarize } from "@/lib/live";34const raw = {5 name: "PRIVATE-HOST", status: "online", cpu: 12.345, memUsedMB: 8192, memTotalMB: 16384, gpu: 40, load: [1.234, 1, 1], uptime: 90061.4,6 thermal: "nominal", netInKBs: 10.26, netOutKBs: 2, diskUsedGB: 50, diskTotalGB: 200, battery: -1, topProcess: "secret-job", users: 3, os: "27.0", hostname: "h.internal",7};89describe("live telemetry sanitiser", () => {10 it("publishes utilisation figures only, keyed by the public id", () => {11 const n = sanitizeNode("node-01", raw);12 expect(n).toEqual({ id: "node-01", online: true, cpu: 12.3, memPct: 50, memUsedGB: 8, memTotalGB: 16, gpu: 40, load1: 1.23, uptimeS: 90061, thermal: "nominal", netInKBs: 10.3, netOutKBs: 2, battery: null });13 expect(JSON.stringify(n)).not.toMatch(/PRIVATE|secret|internal|27\.0/);14 });15 it("reports a missing or offline machine as offline, with no figures", () => {16 for (const r of [undefined, { ...raw, status: "offline" }]) {17 const n = sanitizeNode("node-02", r);18 expect(n.online).toBe(false);19 expect(n.cpu).toBe(0);20 expect(n.gpu).toBeNull();21 }22 });23 it("treats a negative GPU reading as unavailable and clamps percentages", () => {24 const n = sanitizeNode("node-03", { ...raw, gpu: -1, cpu: 250, thermal: "<script>" });25 expect(n.gpu).toBeNull();26 expect(n.cpu).toBe(100);27 expect(n.thermal).toBeNull();28 });29});3031describe("cluster summary", () => {32 const recs = [{ id: "a", cpuCores: 30, gpuCores: 60, unifiedMemoryGB: 96 }, { id: "b", cpuCores: 10, gpuCores: 10, unifiedMemoryGB: 16 }, { id: "c", cpuCores: 10, gpuCores: 10, unifiedMemoryGB: 16 }];33 it("weights CPU by core count and ignores offline machines", () => {34 const live = [35 { ...sanitizeNode("a", { ...raw, cpu: 100, gpu: -1 }) },36 { ...sanitizeNode("b", { ...raw, cpu: 0, gpu: 50 }) },37 { ...sanitizeNode("c", undefined) },38 ];39 const s = summarize(live, recs);40 expect(s.online).toBe(2);41 expect(s.total).toBe(3);42 expect(s.cpu).toBe(75);43 expect(s.gpu).toBe(50);44 expect(s.coresOnline).toBe(40);45 expect(s.memPct).toBe(50);46 });47 it("builds a weighted series over the time buckets present", () => {48 const by = new Map([49 ["a", [{ t: 1, cpu: 100, mem: 50, gpu: null }, { t: 2, cpu: 40, mem: 50, gpu: 10 }]],50 ["b", [{ t: 2, cpu: 0, mem: 50, gpu: 80 }]],51 ]);52 const s = clusterSeries(by, recs);53 expect(s.map((p) => p.t)).toEqual([1, 2]);54 expect(s[0]).toEqual({ t: 1, cpu: 100, mem: 50, gpu: null });55 expect(s[1].cpu).toBe(30);56 expect(s[1].gpu).toBe(20);57 });58});59