1
0

feat: 新增 memory checker,支持系统级内存和交换空间检测

This commit is contained in:
2026-05-27 00:05:06 +08:00
parent 358f8d011a
commit 145bb8fd04
22 changed files with 2348 additions and 14 deletions

View File

@@ -0,0 +1,72 @@
import { describe, expect, test } from "bun:test";
import { normalizeTargetExpect } from "../../../../../src/server/checker/runner/memory/normalize";
describe("normalizeTargetExpect (memory)", () => {
test("无 expect 直接返回", () => {
const target = { id: "test", memory: {}, type: "memory" };
expect(normalizeTargetExpect(target)).toEqual(target);
});
test("expect 为非对象直接返回", () => {
const target = { expect: "not-an-object", id: "test", memory: {}, type: "memory" };
expect(normalizeTargetExpect(target)).toEqual(target);
});
test("字节大小字符串 512MB 转换为数字", () => {
const target = { expect: { usedBytes: "512MB" }, id: "test", memory: {}, type: "memory" };
const result = normalizeTargetExpect(target);
expect((result.expect as Record<string, unknown>)["usedBytes"]).toEqual({ equals: 536870912 });
});
test("字节大小字符串 1GB 转换为数字", () => {
const target = { expect: { totalBytes: "1GB" }, id: "test", memory: {}, type: "memory" };
const result = normalizeTargetExpect(target);
expect((result.expect as Record<string, unknown>)["totalBytes"]).toEqual({ equals: 1073741824 });
});
test("数字字节 matcher 保持不变", () => {
const target = { expect: { usedBytes: 1073741824 }, id: "test", memory: {}, type: "memory" };
const result = normalizeTargetExpect(target);
expect((result.expect as Record<string, unknown>)["usedBytes"]).toEqual({ equals: 1073741824 });
});
test("百分比 matcher 正常展开", () => {
const target = { expect: { usagePercent: 85 }, id: "test", memory: {}, type: "memory" };
const result = normalizeTargetExpect(target);
expect((result.expect as Record<string, unknown>)["usagePercent"]).toEqual({ equals: 85 });
});
test("matcher 对象保持不变", () => {
const target = { expect: { usagePercent: { lte: 85 } }, id: "test", memory: {}, type: "memory" };
const result = normalizeTargetExpect(target);
expect((result.expect as Record<string, unknown>)["usagePercent"]).toEqual({ lte: 85 });
});
test("字节 matcher 对象内字符串转换", () => {
const target = { expect: { usedBytes: { gte: "512MB" } }, id: "test", memory: {}, type: "memory" };
const result = normalizeTargetExpect(target);
expect((result.expect as Record<string, unknown>)["usedBytes"]).toEqual({ gte: 536870912 });
});
test("多个字段同时处理", () => {
const target = {
expect: { freePercent: 25, totalBytes: "16GB", usagePercent: { lte: 85 } },
id: "test",
memory: {},
type: "memory",
};
const result = normalizeTargetExpect(target);
const expectObj = result.expect as Record<string, unknown>;
expect(expectObj["freePercent"]).toEqual({ equals: 25 });
expect(expectObj["totalBytes"]).toEqual({ equals: 17179869184 });
expect(expectObj["usagePercent"]).toEqual({ lte: 85 });
});
});
describe("normalizeTargetExpect (memory) 错误", () => {
test("非法大小字符串抛出", () => {
const target = { expect: { usedBytes: "abc" }, id: "test", memory: {}, type: "memory" };
expect(() => normalizeTargetExpect(target)).toThrow();
});
});