mirror of
https://github.com/Termix-SSH/Termix.git
synced 2026-10-29 06:35:24 +00:00
46 lines
1.4 KiB
TypeScript
46 lines
1.4 KiB
TypeScript
import { describe, it, expect } from "vitest";
|
|
import {
|
|
parseCpuLine,
|
|
parseDarwinCpuUsageLine,
|
|
} from "../../../src/backend/widgets/cpu-collector.js";
|
|
|
|
describe("parseCpuLine", () => {
|
|
it("parses a standard /proc/stat cpu line", () => {
|
|
// user nice system idle iowait irq softirq
|
|
const result = parseCpuLine("cpu 100 0 50 800 30 0 20");
|
|
expect(result).toBeDefined();
|
|
// idle = idle(800) + iowait(30)
|
|
expect(result?.idle).toBe(830);
|
|
// total = sum of all fields
|
|
expect(result?.total).toBe(100 + 0 + 50 + 800 + 30 + 0 + 20);
|
|
});
|
|
|
|
it("tolerates leading/trailing whitespace", () => {
|
|
const result = parseCpuLine(" cpu 1 2 3 4 ");
|
|
expect(result?.total).toBe(10);
|
|
expect(result?.idle).toBe(4);
|
|
});
|
|
|
|
it("returns undefined for non-cpu lines", () => {
|
|
expect(parseCpuLine("cpu0 1 2 3 4")).toBeUndefined();
|
|
expect(parseCpuLine("intr 12345")).toBeUndefined();
|
|
});
|
|
|
|
it("returns undefined when there are fewer than 4 numeric fields", () => {
|
|
expect(parseCpuLine("cpu 1 2 3")).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
describe("parseDarwinCpuUsageLine", () => {
|
|
it("parses macOS `top -l 1` CPU usage line", () => {
|
|
const result = parseDarwinCpuUsageLine(
|
|
"CPU usage: 12.34% user, 5.66% sys, 82.00% idle",
|
|
);
|
|
expect(result?.percent).toBeCloseTo(18.0, 5);
|
|
});
|
|
|
|
it("returns undefined for unrelated lines", () => {
|
|
expect(parseDarwinCpuUsageLine("Processes: 400 total")).toBeUndefined();
|
|
});
|
|
});
|