feat(FN-1227): add mesh state and system metrics support to CentralCore

- Add SystemMetrics, PeerNode, NodeMeshState, and MeshDiscovery types and export new mesh/metrics APIs from @fusion/core
- Introduce collectSystemMetrics with CPU, memory, storage, and uptime collection (including check-disk-space integration) plus dedicated unit tests
- Bump central DB schema to v3 with nodes.systemMetrics/knownPeers and peerNodes table, including migration coverage for v2->v3 upgrades
- Extend CentralCore with node metrics updates, peer register/unregister/list operations, mesh snapshot reporting, and event emissions backed by expanded test coverage
This commit is contained in:
gsxdsm
2026-04-08 11:21:18 -07:00
parent 0e200e2483
commit ff7f35c6c0
10 changed files with 851 additions and 2 deletions

View File

@@ -0,0 +1,71 @@
import { describe, it, expect, beforeEach, vi } from "vitest";
import { collectSystemMetrics } from "./system-metrics.js";
const { checkDiskSpaceMock } = vi.hoisted(() => ({
checkDiskSpaceMock: vi.fn(),
}));
vi.mock("check-disk-space", () => ({
default: checkDiskSpaceMock,
}));
describe("collectSystemMetrics", () => {
beforeEach(() => {
vi.clearAllMocks();
checkDiskSpaceMock.mockResolvedValue({
diskPath: "/",
free: 250_000,
size: 1_000_000,
});
});
it("returns a valid SystemMetrics object", async () => {
const metrics = await collectSystemMetrics();
expect(metrics).toEqual(
expect.objectContaining({
cpuUsage: expect.any(Number),
memoryUsed: expect.any(Number),
memoryTotal: expect.any(Number),
storageUsed: expect.any(Number),
storageTotal: expect.any(Number),
uptime: expect.any(Number),
reportedAt: expect.any(String),
}),
);
});
it("returns cpuUsage between 0 and 100", async () => {
const metrics = await collectSystemMetrics();
expect(metrics.cpuUsage).toBeGreaterThanOrEqual(0);
expect(metrics.cpuUsage).toBeLessThanOrEqual(100);
});
it("returns memoryUsed less than or equal to memoryTotal", async () => {
const metrics = await collectSystemMetrics();
expect(metrics.memoryUsed).toBeLessThanOrEqual(metrics.memoryTotal);
});
it("returns storageUsed less than or equal to storageTotal", async () => {
const metrics = await collectSystemMetrics();
expect(metrics.storageUsed).toBeLessThanOrEqual(metrics.storageTotal);
});
it("returns uptime greater than 0", async () => {
const metrics = await collectSystemMetrics();
expect(metrics.uptime).toBeGreaterThan(0);
});
it("returns a valid ISO timestamp in reportedAt", async () => {
const metrics = await collectSystemMetrics();
expect(new Date(metrics.reportedAt).toISOString()).toBe(metrics.reportedAt);
});
it("passes dbPath through to check-disk-space", async () => {
const customPath = "/tmp/kb-metrics-db";
await collectSystemMetrics(customPath);
expect(checkDiskSpaceMock).toHaveBeenCalledWith(customPath);
});
});