mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 16:08:56 +00:00
* fix(engine): start nvidia-smi, vm_stat, ps and tasklist from trusted folders only The renderer resolves these helpers through the shared trusted lookup and runs the absolute path, so a program with the same name in a project folder is never started in their place. * test(engine): memory and import tests follow helpers started by resolved path
125 lines
4.6 KiB
TypeScript
125 lines
4.6 KiB
TypeScript
// fallow-ignore-file code-duplication
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
beforeEach(() => {
|
|
vi.resetModules();
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
describe("getAvailableMemoryMb", () => {
|
|
it("parses vm_stat on macOS to compute available memory", async () => {
|
|
vi.doMock("node:os", async () => ({
|
|
...(await vi.importActual<typeof import("node:os")>("node:os")),
|
|
platform: () => "darwin",
|
|
freemem: () => 100 * 1024 * 1024,
|
|
}));
|
|
vi.doMock("@hyperframes/parsers/ff-binaries", async () => ({
|
|
...(await vi.importActual<object>("@hyperframes/parsers/ff-binaries")),
|
|
findTrustedExecutable: (name: string) => `/usr/bin/${name}`,
|
|
}));
|
|
const execFileSync = vi.fn(() =>
|
|
[
|
|
"Mach Virtual Memory Statistics: (page size of 16384 bytes)",
|
|
"Pages free: 5000.",
|
|
"Pages active: 200000.",
|
|
"Pages inactive: 100000.",
|
|
"Pages speculative: 2000.",
|
|
"Pages throttled: 0.",
|
|
"Pages wired down: 150000.",
|
|
"Pages purgeable: 3000.",
|
|
].join("\n"),
|
|
);
|
|
vi.doMock("node:child_process", async () => ({
|
|
...(await vi.importActual<typeof import("node:child_process")>("node:child_process")),
|
|
execFileSync,
|
|
}));
|
|
|
|
const { getAvailableMemoryMb } = await import("./systemMemory.js");
|
|
const result = getAvailableMemoryMb();
|
|
|
|
expect(execFileSync).toHaveBeenCalledWith("/usr/bin/vm_stat", [], expect.anything());
|
|
// (5000 + 100000 + 3000 + 2000) * 16384 = 110000 * 16384 = 1,802,240,000 bytes
|
|
// 1,802,240,000 / (1024 * 1024) = ~1718 MB
|
|
expect(result).toBe(Math.trunc((110000 * 16384) / (1024 * 1024)));
|
|
});
|
|
|
|
it("falls back to freemem on macOS when vm_stat fails", async () => {
|
|
vi.doMock("node:os", async () => ({
|
|
...(await vi.importActual<typeof import("node:os")>("node:os")),
|
|
platform: () => "darwin",
|
|
freemem: () => 4 * 1024 * 1024 * 1024,
|
|
}));
|
|
vi.doMock("@hyperframes/parsers/ff-binaries", async () => ({
|
|
...(await vi.importActual<object>("@hyperframes/parsers/ff-binaries")),
|
|
findTrustedExecutable: (name: string) => `/usr/bin/${name}`,
|
|
}));
|
|
vi.doMock("node:child_process", async () => ({
|
|
...(await vi.importActual<typeof import("node:child_process")>("node:child_process")),
|
|
execFileSync: () => {
|
|
throw new Error("vm_stat not found");
|
|
},
|
|
}));
|
|
|
|
const { getAvailableMemoryMb } = await import("./systemMemory.js");
|
|
expect(getAvailableMemoryMb()).toBe(4096);
|
|
});
|
|
|
|
it("reads MemAvailable from /proc/meminfo on Linux", async () => {
|
|
vi.doMock("node:os", async () => ({
|
|
...(await vi.importActual<typeof import("node:os")>("node:os")),
|
|
platform: () => "linux",
|
|
freemem: () => 100 * 1024 * 1024,
|
|
}));
|
|
vi.doMock("node:fs", async () => ({
|
|
...(await vi.importActual<typeof import("node:fs")>("node:fs")),
|
|
readFileSync: (path: string, _enc: string) => {
|
|
if (path === "/proc/meminfo") {
|
|
return [
|
|
"MemTotal: 16384000 kB",
|
|
"MemFree: 512000 kB",
|
|
"MemAvailable: 8192000 kB",
|
|
"Buffers: 256000 kB",
|
|
].join("\n");
|
|
}
|
|
throw new Error(`unexpected path: ${path}`);
|
|
},
|
|
}));
|
|
|
|
const { getAvailableMemoryMb } = await import("./systemMemory.js");
|
|
// 8192000 kB / 1024 = 8000 MB
|
|
expect(getAvailableMemoryMb()).toBe(8000);
|
|
});
|
|
|
|
it("falls back to freemem on Linux when /proc/meminfo is unreadable", async () => {
|
|
vi.doMock("node:os", async () => ({
|
|
...(await vi.importActual<typeof import("node:os")>("node:os")),
|
|
platform: () => "linux",
|
|
freemem: () => 2 * 1024 * 1024 * 1024,
|
|
}));
|
|
vi.doMock("node:fs", async () => ({
|
|
...(await vi.importActual<typeof import("node:fs")>("node:fs")),
|
|
readFileSync: (path: string) => {
|
|
if (path === "/proc/meminfo") throw new Error("ENOENT");
|
|
throw new Error(`unexpected path: ${path}`);
|
|
},
|
|
}));
|
|
|
|
const { getAvailableMemoryMb } = await import("./systemMemory.js");
|
|
expect(getAvailableMemoryMb()).toBe(2048);
|
|
});
|
|
|
|
it("falls back to freemem on unsupported platforms", async () => {
|
|
vi.doMock("node:os", async () => ({
|
|
...(await vi.importActual<typeof import("node:os")>("node:os")),
|
|
platform: () => "win32",
|
|
freemem: () => 6 * 1024 * 1024 * 1024,
|
|
}));
|
|
|
|
const { getAvailableMemoryMb } = await import("./systemMemory.js");
|
|
expect(getAvailableMemoryMb()).toBe(6144);
|
|
});
|
|
});
|