- 类型标识符 memory → mem - 类名 MemoryChecker → MemChecker - 内部类型名统一 Memory* → Mem* - 内部函数名统一 *Memory* → *Mem* - 目录重命名 memory/ → mem/(源码、测试、文档) - 配置键 memory: → mem: - 重新生成 probe-config.schema.json - 保留中文"内存"用户提示 破坏性变更:无向后兼容
88 lines
3.2 KiB
TypeScript
88 lines
3.2 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
|
|
import type { RawTargetConfig } from "../../../../../src/server/checker/types";
|
|
|
|
import { validateMemConfig } from "../../../../../src/server/checker/runner/mem/validate";
|
|
|
|
function validate(target: RawTargetConfig) {
|
|
return validateMemConfig({ targets: [target] });
|
|
}
|
|
|
|
describe("validateMemConfig", () => {
|
|
test("有效配置无错误", () => {
|
|
expect(validate({ id: "mem-test", mem: {}, type: "mem" })).toEqual([]);
|
|
});
|
|
|
|
test("缺少 mem 配置分组", () => {
|
|
const issues = validate({ id: "mem-test", type: "mem" });
|
|
expect(issues.some((i) => i.path.endsWith("mem") && i.code === "required")).toBe(true);
|
|
});
|
|
|
|
test("mem 未知字段报错", () => {
|
|
const issues = validate({ id: "mem-test", mem: { extra: true }, type: "mem" });
|
|
expect(issues.some((i) => i.path.endsWith("extra") && i.code === "unknown-field")).toBe(true);
|
|
});
|
|
|
|
test("expect 未知字段报错", () => {
|
|
const issues = validate({ expect: { logicalCoreCount: { gte: 4 } }, id: "mem-test", mem: {}, type: "mem" });
|
|
expect(issues.some((i) => i.path.endsWith("logicalCoreCount") && i.code === "unknown-field")).toBe(true);
|
|
});
|
|
|
|
test("expect 合法 ValueMatcher 通过", () => {
|
|
const issues = validate({
|
|
expect: { usagePercent: { lte: 85 }, usedBytes: { lte: 8589934592 } },
|
|
id: "mem-test",
|
|
mem: {},
|
|
type: "mem",
|
|
});
|
|
expect(issues.filter((i) => i.path.includes("expect"))).toEqual([]);
|
|
});
|
|
|
|
test("expect 非法 ValueMatcher 报错", () => {
|
|
const issues = validate({ expect: { usagePercent: [1, 2] }, id: "mem-test", mem: {}, type: "mem" });
|
|
expect(issues.some((i) => i.path.includes("usagePercent"))).toBe(true);
|
|
});
|
|
|
|
test("expect 合法字节大小字符串通过", () => {
|
|
const issues = validate({ expect: { usedBytes: "512MB" }, id: "mem-test", mem: {}, type: "mem" });
|
|
expect(issues.filter((i) => i.path.includes("usedBytes"))).toEqual([]);
|
|
});
|
|
|
|
test("expect 非法字节大小字符串报错", () => {
|
|
const issues = validate({ expect: { usedBytes: "abc" }, id: "mem-test", mem: {}, type: "mem" });
|
|
expect(issues.some((i) => i.path.includes("usedBytes") && i.message.includes("字节大小"))).toBe(true);
|
|
});
|
|
|
|
test("expect 所有合法字段通过", () => {
|
|
const issues = validate({
|
|
expect: {
|
|
activeBytes: { lte: 8589934592 },
|
|
activePercent: { lte: 80 },
|
|
availableBytes: { gte: 4294967296 },
|
|
availablePercent: { gte: 20 },
|
|
buffcacheBytes: { lte: 2147483648 },
|
|
durationMs: { lte: 5000 },
|
|
freeBytes: { gte: 2147483648 },
|
|
freePercent: { gte: 15 },
|
|
swapFreeBytes: { gte: 0 },
|
|
swapTotalBytes: { lte: 4294967296 },
|
|
swapUsagePercent: { lte: 50 },
|
|
swapUsedBytes: { lte: 2147483648 },
|
|
totalBytes: { equals: 17179869184 },
|
|
usagePercent: { lte: 85 },
|
|
usedBytes: { lte: 8589934592 },
|
|
usedPercent: { lte: 90 },
|
|
},
|
|
id: "mem-test",
|
|
mem: {},
|
|
type: "mem",
|
|
});
|
|
expect(issues).toEqual([]);
|
|
});
|
|
|
|
test("非 mem type 的 target 不校验", () => {
|
|
const issues = validate({ id: "other-test", type: "http" });
|
|
expect(issues).toEqual([]);
|
|
});
|
|
});
|