|
| 1 | +import { describe, test, expect, vi, beforeEach, afterEach } from "vitest"; |
| 2 | +import { ResourceMonitor } from "./resourceMonitor.js"; |
| 3 | + |
| 4 | +// Mock node:child_process |
| 5 | +vi.mock("node:child_process", () => ({ |
| 6 | + exec: vi.fn(), |
| 7 | +})); |
| 8 | + |
| 9 | +// Mock node:v8 |
| 10 | +vi.mock("node:v8", () => ({ |
| 11 | + getHeapStatistics: vi.fn(() => ({ |
| 12 | + total_heap_size: 50 * 1024 * 1024, |
| 13 | + total_heap_size_executable: 0, |
| 14 | + total_physical_size: 50 * 1024 * 1024, |
| 15 | + total_available_size: 100 * 1024 * 1024, |
| 16 | + used_heap_size: 25 * 1024 * 1024, |
| 17 | + heap_size_limit: 200 * 1024 * 1024, |
| 18 | + malloced_memory: 0, |
| 19 | + peak_malloced_memory: 0, |
| 20 | + does_zap_garbage: 0, |
| 21 | + number_of_native_contexts: 1, |
| 22 | + number_of_detached_contexts: 0, |
| 23 | + total_global_handles_size: 0, |
| 24 | + used_global_handles_size: 0, |
| 25 | + external_memory: 0, |
| 26 | + })), |
| 27 | +})); |
| 28 | + |
| 29 | +import os from "node:os"; |
| 30 | +import { exec } from "node:child_process"; |
| 31 | + |
| 32 | +const mockedExec = vi.mocked(exec); |
| 33 | + |
| 34 | +describe("ResourceMonitor", () => { |
| 35 | + beforeEach(() => { |
| 36 | + vi.clearAllMocks(); |
| 37 | + |
| 38 | + // Default mocks for os module |
| 39 | + vi.spyOn(os, "totalmem").mockReturnValue(4 * 1024 * 1024 * 1024); // 4 GB |
| 40 | + vi.spyOn(os, "freemem").mockReturnValue(2 * 1024 * 1024 * 1024); // 2 GB free |
| 41 | + vi.spyOn(os, "cpus").mockReturnValue([ |
| 42 | + { model: "test", speed: 2400, times: { user: 0, nice: 0, sys: 0, idle: 0, irq: 0 } }, |
| 43 | + { model: "test", speed: 2400, times: { user: 0, nice: 0, sys: 0, idle: 0, irq: 0 } }, |
| 44 | + ]); |
| 45 | + |
| 46 | + // Mock process.memoryUsage |
| 47 | + vi.spyOn(process, "memoryUsage").mockReturnValue({ |
| 48 | + rss: 100 * 1024 * 1024, // 100 MB RSS |
| 49 | + heapTotal: 50 * 1024 * 1024, |
| 50 | + heapUsed: 25 * 1024 * 1024, |
| 51 | + external: 0, |
| 52 | + arrayBuffers: 0, |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + afterEach(() => { |
| 57 | + vi.restoreAllMocks(); |
| 58 | + }); |
| 59 | + |
| 60 | + test("should return properly formatted system memory metrics", async () => { |
| 61 | + // Mock disk metrics (du command fails on non-Linux, so simulate failure) |
| 62 | + mockedExec.mockImplementation((( |
| 63 | + cmd: string, |
| 64 | + callback?: (error: Error | null, result: { stdout: string; stderr: string }) => void |
| 65 | + ) => { |
| 66 | + if (callback) { |
| 67 | + callback(new Error("Command not available"), { stdout: "", stderr: "" }); |
| 68 | + } |
| 69 | + return {} as any; |
| 70 | + }) as any); |
| 71 | + |
| 72 | + const monitor = new ResourceMonitor({ |
| 73 | + dirName: "/tmp", |
| 74 | + ctx: {}, |
| 75 | + verbose: false, |
| 76 | + }); |
| 77 | + |
| 78 | + const payload = await monitor.getResourceSnapshotPayload(); |
| 79 | + |
| 80 | + // System memory should reflect our mocked values |
| 81 | + // 4GB total, 2GB free = 50% used |
| 82 | + expect(parseFloat(payload.system.memory.percentUsed)).toBeCloseTo(50.0, 0); |
| 83 | + expect(parseFloat(payload.system.memory.freeGB)).toBeCloseTo(2.0, 0); |
| 84 | + }); |
| 85 | + |
| 86 | + test("should calculate node process memory percentage correctly", async () => { |
| 87 | + mockedExec.mockImplementation((( |
| 88 | + cmd: string, |
| 89 | + callback?: (error: Error | null, result: { stdout: string; stderr: string }) => void |
| 90 | + ) => { |
| 91 | + if (callback) { |
| 92 | + callback(new Error("Command not available"), { stdout: "", stderr: "" }); |
| 93 | + } |
| 94 | + return {} as any; |
| 95 | + }) as any); |
| 96 | + |
| 97 | + const monitor = new ResourceMonitor({ |
| 98 | + dirName: "/tmp", |
| 99 | + ctx: {}, |
| 100 | + verbose: false, |
| 101 | + }); |
| 102 | + |
| 103 | + const payload = await monitor.getResourceSnapshotPayload(); |
| 104 | + |
| 105 | + // 100 MB RSS out of 4 GB total = ~2.44% |
| 106 | + const nodeMemPercent = parseFloat(payload.process.node.memoryUsagePercent); |
| 107 | + expect(nodeMemPercent).toBeCloseTo(2.4, 0); |
| 108 | + |
| 109 | + // RSS should be ~100 MB |
| 110 | + const nodeMemMB = parseFloat(payload.process.node.memoryUsageMB); |
| 111 | + expect(nodeMemMB).toBeCloseTo(100.0, 0); |
| 112 | + }); |
| 113 | + |
| 114 | + test("should calculate heap usage percentage correctly", async () => { |
| 115 | + mockedExec.mockImplementation((( |
| 116 | + cmd: string, |
| 117 | + callback?: (error: Error | null, result: { stdout: string; stderr: string }) => void |
| 118 | + ) => { |
| 119 | + if (callback) { |
| 120 | + callback(new Error("Command not available"), { stdout: "", stderr: "" }); |
| 121 | + } |
| 122 | + return {} as any; |
| 123 | + }) as any); |
| 124 | + |
| 125 | + const monitor = new ResourceMonitor({ |
| 126 | + dirName: "/tmp", |
| 127 | + ctx: {}, |
| 128 | + verbose: false, |
| 129 | + }); |
| 130 | + |
| 131 | + const payload = await monitor.getResourceSnapshotPayload(); |
| 132 | + |
| 133 | + // 25 MB used / 200 MB limit = 12.5% |
| 134 | + const heapPercent = parseFloat(payload.process.node.heapUsagePercent); |
| 135 | + expect(heapPercent).toBeCloseTo(12.5, 0); |
| 136 | + expect(payload.process.node.isNearHeapLimit).toBe(false); |
| 137 | + }); |
| 138 | + |
| 139 | + test("should detect near heap limit condition", async () => { |
| 140 | + // Override getHeapStatistics to return near-limit values |
| 141 | + const { getHeapStatistics } = await import("node:v8"); |
| 142 | + vi.mocked(getHeapStatistics).mockReturnValue({ |
| 143 | + total_heap_size: 180 * 1024 * 1024, |
| 144 | + total_heap_size_executable: 0, |
| 145 | + total_physical_size: 180 * 1024 * 1024, |
| 146 | + total_available_size: 20 * 1024 * 1024, |
| 147 | + used_heap_size: 170 * 1024 * 1024, // 85% of 200MB limit |
| 148 | + heap_size_limit: 200 * 1024 * 1024, |
| 149 | + malloced_memory: 0, |
| 150 | + peak_malloced_memory: 0, |
| 151 | + does_zap_garbage: 0, |
| 152 | + number_of_native_contexts: 1, |
| 153 | + number_of_detached_contexts: 0, |
| 154 | + total_global_handles_size: 0, |
| 155 | + used_global_handles_size: 0, |
| 156 | + external_memory: 0, |
| 157 | + }); |
| 158 | + |
| 159 | + mockedExec.mockImplementation((( |
| 160 | + cmd: string, |
| 161 | + callback?: (error: Error | null, result: { stdout: string; stderr: string }) => void |
| 162 | + ) => { |
| 163 | + if (callback) { |
| 164 | + callback(new Error("Command not available"), { stdout: "", stderr: "" }); |
| 165 | + } |
| 166 | + return {} as any; |
| 167 | + }) as any); |
| 168 | + |
| 169 | + const monitor = new ResourceMonitor({ |
| 170 | + dirName: "/tmp", |
| 171 | + ctx: {}, |
| 172 | + verbose: false, |
| 173 | + }); |
| 174 | + |
| 175 | + const payload = await monitor.getResourceSnapshotPayload(); |
| 176 | + |
| 177 | + // 170/200 = 85% > 80% threshold |
| 178 | + expect(payload.process.node.isNearHeapLimit).toBe(true); |
| 179 | + }); |
| 180 | + |
| 181 | + test("should include constraint information", async () => { |
| 182 | + mockedExec.mockImplementation((( |
| 183 | + cmd: string, |
| 184 | + callback?: (error: Error | null, result: { stdout: string; stderr: string }) => void |
| 185 | + ) => { |
| 186 | + if (callback) { |
| 187 | + callback(new Error("Command not available"), { stdout: "", stderr: "" }); |
| 188 | + } |
| 189 | + return {} as any; |
| 190 | + }) as any); |
| 191 | + |
| 192 | + const monitor = new ResourceMonitor({ |
| 193 | + dirName: "/tmp", |
| 194 | + ctx: {}, |
| 195 | + verbose: false, |
| 196 | + }); |
| 197 | + |
| 198 | + const payload = await monitor.getResourceSnapshotPayload(); |
| 199 | + |
| 200 | + expect(payload.constraints).toBeDefined(); |
| 201 | + expect(payload.constraints.cpu).toBe(2); // 2 CPUs mocked |
| 202 | + expect(payload.constraints.memoryGB).toBe(4); // 4 GB mocked |
| 203 | + expect(payload.timestamp).toBeDefined(); |
| 204 | + }); |
| 205 | +}); |
0 commit comments