Files
fusion/packages/core/src/system-metrics.test.ts
gsxdsm 9830ea96c6 fix(ci): fix test failures from recent feature additions
- Add ChatStore mock to all dashboard route tests that mock @fusion/core,
  since server.ts now instantiates ChatStore(store.getFusionDir(), ...)
- Add getFusionDir to createMockStore in server.test.ts
- Gate AI session cleanup scheduling behind shouldScheduleAiSessionCleanup()
  (returns false in test env) to prevent open handle warnings
- Fix desktop tests: DASHBOARD_URL is now exported as a function alias,
  update assertions to call DASHBOARD_URL() instead of using as string
- Add node:os mocks to system-metrics.test.ts for deterministic results
- Replace hardcoded maxWorkers=16 with availableParallelism()-based
  calculation in all vitest configs to prevent OOM on 2-core CI runners
- Add --workspace-concurrency=2 to pnpm test commands
- Fix TaskCard tests: update mission badge title assertions to full titles
- Remove unused /api/mesh/state route
- Fix plugin-auto-label: add isError field, async onTaskCreated, "tests" keyword
- Fix plugin-ci-status: add module-level logger, tighten test assertions

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-10 08:01:03 -07:00

97 lines
2.7 KiB
TypeScript

import { describe, it, expect, beforeEach, vi } from "vitest";
import { collectSystemMetrics } from "./system-metrics.js";
const { checkDiskSpaceMock, cpusMock, totalmemMock, freememMock, uptimeMock } = vi.hoisted(() => ({
checkDiskSpaceMock: vi.fn(),
cpusMock: vi.fn(),
totalmemMock: vi.fn(),
freememMock: vi.fn(),
uptimeMock: vi.fn(),
}));
vi.mock("check-disk-space", () => ({
default: checkDiskSpaceMock,
}));
vi.mock("node:os", () => ({
cpus: cpusMock,
totalmem: totalmemMock,
freemem: freememMock,
uptime: uptimeMock,
}));
describe("collectSystemMetrics", () => {
beforeEach(() => {
vi.clearAllMocks();
cpusMock.mockReturnValue([
{
times: {
user: 100,
nice: 0,
sys: 50,
idle: 150,
irq: 0,
},
},
]);
totalmemMock.mockReturnValue(16_000);
freememMock.mockReturnValue(6_000);
uptimeMock.mockReturnValue(12.345);
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);
});
});