refactor: 将 memory checker 重命名为 mem
- 类型标识符 memory → mem - 类名 MemoryChecker → MemChecker - 内部类型名统一 Memory* → Mem* - 内部函数名统一 *Memory* → *Mem* - 目录重命名 memory/ → mem/(源码、测试、文档) - 配置键 memory: → mem: - 重新生成 probe-config.schema.json - 保留中文"内存"用户提示 破坏性变更:无向后兼容
This commit is contained in:
@@ -2,7 +2,7 @@ import type { Systeminformation } from "systeminformation";
|
||||
|
||||
import { describe, expect, test } from "bun:test";
|
||||
|
||||
import { calculateMemoryStats } from "../../../../../src/server/checker/runner/memory/calculate";
|
||||
import { calculateMemStats } from "../../../../../src/server/checker/runner/mem/calculate";
|
||||
|
||||
function makeMemData(overrides: Partial<Systeminformation.MemData> = {}): Systeminformation.MemData {
|
||||
return {
|
||||
@@ -25,56 +25,56 @@ function makeMemData(overrides: Partial<Systeminformation.MemData> = {}): System
|
||||
};
|
||||
}
|
||||
|
||||
describe("calculateMemoryStats", () => {
|
||||
describe("calculateMemStats", () => {
|
||||
test("usagePercent = activeBytes / totalBytes * 100", () => {
|
||||
const stats = calculateMemoryStats(makeMemData({ active: 4294967296, total: 8589934592 }));
|
||||
const stats = calculateMemStats(makeMemData({ active: 4294967296, total: 8589934592 }));
|
||||
expect(stats.usagePercent).toBe(50);
|
||||
});
|
||||
|
||||
test("usedPercent = usedBytes / totalBytes * 100", () => {
|
||||
const stats = calculateMemoryStats(makeMemData({ total: 8589934592, used: 6442450944 }));
|
||||
const stats = calculateMemStats(makeMemData({ total: 8589934592, used: 6442450944 }));
|
||||
expect(stats.usedPercent).toBe(75);
|
||||
});
|
||||
|
||||
test("freePercent = freeBytes / totalBytes * 100", () => {
|
||||
const stats = calculateMemoryStats(makeMemData({ free: 2147483648, total: 8589934592 }));
|
||||
const stats = calculateMemStats(makeMemData({ free: 2147483648, total: 8589934592 }));
|
||||
expect(stats.freePercent).toBe(25);
|
||||
});
|
||||
|
||||
test("activePercent = activeBytes / totalBytes * 100", () => {
|
||||
const stats = calculateMemoryStats(makeMemData({ active: 3221225472, total: 8589934592 }));
|
||||
const stats = calculateMemStats(makeMemData({ active: 3221225472, total: 8589934592 }));
|
||||
expect(stats.activePercent).toBe(37.5);
|
||||
});
|
||||
|
||||
test("availablePercent = availableBytes / totalBytes * 100", () => {
|
||||
const stats = calculateMemoryStats(makeMemData({ available: 6442450944, total: 8589934592 }));
|
||||
const stats = calculateMemStats(makeMemData({ available: 6442450944, total: 8589934592 }));
|
||||
expect(stats.availablePercent).toBe(75);
|
||||
});
|
||||
|
||||
test("保留 1 位小数", () => {
|
||||
const stats = calculateMemoryStats(makeMemData({ active: 3000000000, total: 8000000000 }));
|
||||
const stats = calculateMemStats(makeMemData({ active: 3000000000, total: 8000000000 }));
|
||||
expect(stats.usagePercent).toBe(37.5);
|
||||
});
|
||||
|
||||
test("round1 处理需要四舍五入的情况", () => {
|
||||
const stats = calculateMemoryStats(makeMemData({ active: 3333333333, total: 10000000000 }));
|
||||
const stats = calculateMemStats(makeMemData({ active: 3333333333, total: 10000000000 }));
|
||||
expect(stats.usagePercent).toBe(33.3);
|
||||
});
|
||||
|
||||
test("total 为 0 时百分比为 0", () => {
|
||||
const stats = calculateMemoryStats(makeMemData({ active: 0, available: 0, free: 0, total: 0, used: 0 }));
|
||||
const stats = calculateMemStats(makeMemData({ active: 0, available: 0, free: 0, total: 0, used: 0 }));
|
||||
expect(stats.usagePercent).toBe(0);
|
||||
expect(stats.usedPercent).toBe(0);
|
||||
expect(stats.freePercent).toBe(0);
|
||||
});
|
||||
|
||||
test("buffcacheBytes 为 null 映射", () => {
|
||||
const stats = calculateMemoryStats(makeMemData({ buffcache: 0 }));
|
||||
const stats = calculateMemStats(makeMemData({ buffcache: 0 }));
|
||||
expect(stats.buffcacheBytes).toBe(0);
|
||||
});
|
||||
|
||||
test("buffcacheBytes 为正数时保留", () => {
|
||||
const stats = calculateMemoryStats(makeMemData({ buffcache: 1073741824 }));
|
||||
const stats = calculateMemStats(makeMemData({ buffcache: 1073741824 }));
|
||||
expect(stats.buffcacheBytes).toBe(1073741824);
|
||||
});
|
||||
|
||||
@@ -86,7 +86,7 @@ describe("calculateMemoryStats", () => {
|
||||
total: 4000,
|
||||
used: 3500,
|
||||
});
|
||||
const stats = calculateMemoryStats(data);
|
||||
const stats = calculateMemStats(data);
|
||||
expect(stats.activeBytes).toBe(1000);
|
||||
expect(stats.availableBytes).toBe(2000);
|
||||
expect(stats.freeBytes).toBe(3000);
|
||||
@@ -95,9 +95,9 @@ describe("calculateMemoryStats", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("calculateMemoryStats swap", () => {
|
||||
describe("calculateMemStats swap", () => {
|
||||
test("swap 不可用:swaptotal=0 时 swapUsagePercent=null", () => {
|
||||
const stats = calculateMemoryStats(makeMemData({ swapfree: 0, swaptotal: 0, swapused: 0 }));
|
||||
const stats = calculateMemStats(makeMemData({ swapfree: 0, swaptotal: 0, swapused: 0 }));
|
||||
expect(stats.swapUsagePercent).toBe(null);
|
||||
expect(stats.swapTotalBytes).toBe(0);
|
||||
expect(stats.swapUsedBytes).toBe(0);
|
||||
@@ -105,14 +105,12 @@ describe("calculateMemoryStats swap", () => {
|
||||
});
|
||||
|
||||
test("swap 总量为 0,swapUsagePercent 为 null(不是 0)", () => {
|
||||
const stats = calculateMemoryStats(makeMemData({ swaptotal: 0 }));
|
||||
const stats = calculateMemStats(makeMemData({ swaptotal: 0 }));
|
||||
expect(stats.swapUsagePercent).toBe(null);
|
||||
});
|
||||
|
||||
test("swap 已使用", () => {
|
||||
const stats = calculateMemoryStats(
|
||||
makeMemData({ swapfree: 1073741824, swaptotal: 4294967296, swapused: 3221225472 }),
|
||||
);
|
||||
const stats = calculateMemStats(makeMemData({ swapfree: 1073741824, swaptotal: 4294967296, swapused: 3221225472 }));
|
||||
expect(stats.swapUsagePercent).toBe(75);
|
||||
expect(stats.swapTotalBytes).toBe(4294967296);
|
||||
expect(stats.swapUsedBytes).toBe(3221225472);
|
||||
@@ -120,21 +118,21 @@ describe("calculateMemoryStats swap", () => {
|
||||
});
|
||||
|
||||
test("swap 未使用:swapUsagePercent=0(不是 null)", () => {
|
||||
const stats = calculateMemoryStats(makeMemData({ swapfree: 4294967296, swaptotal: 4294967296, swapused: 0 }));
|
||||
const stats = calculateMemStats(makeMemData({ swapfree: 4294967296, swaptotal: 4294967296, swapused: 0 }));
|
||||
expect(stats.swapUsagePercent).toBe(0);
|
||||
expect(stats.swapUsedBytes).toBe(0);
|
||||
expect(stats.swapFreeBytes).toBe(4294967296);
|
||||
});
|
||||
|
||||
test("swap 部分使用保留 1 位小数", () => {
|
||||
const stats = calculateMemoryStats(
|
||||
const stats = calculateMemStats(
|
||||
makeMemData({ swapfree: 3000000000, swaptotal: 10000000000, swapused: 7000000000 }),
|
||||
);
|
||||
expect(stats.swapUsagePercent).toBe(70);
|
||||
});
|
||||
|
||||
test("swap 合法 0 不被转换为 null", () => {
|
||||
const stats = calculateMemoryStats(makeMemData({ swapfree: 4294967296, swaptotal: 4294967296, swapused: 0 }));
|
||||
const stats = calculateMemStats(makeMemData({ swapfree: 4294967296, swaptotal: 4294967296, swapused: 0 }));
|
||||
expect(stats.swapUsedBytes).toBe(0);
|
||||
expect(stats.swapUsagePercent).toBe(0);
|
||||
});
|
||||
@@ -4,7 +4,7 @@ import { describe, expect, test } from "bun:test";
|
||||
|
||||
import type { RawTargetConfig } from "../../../../../src/server/checker/types";
|
||||
|
||||
import { MemoryChecker } from "../../../../../src/server/checker/runner/memory/execute";
|
||||
import { MemChecker } from "../../../../../src/server/checker/runner/mem/execute";
|
||||
|
||||
function makeMemData(overrides: Partial<Systeminformation.MemData> = {}): Systeminformation.MemData {
|
||||
return {
|
||||
@@ -38,17 +38,17 @@ function makeResolveContext(
|
||||
};
|
||||
}
|
||||
|
||||
describe("MemoryChecker resolve", () => {
|
||||
const checker = new MemoryChecker();
|
||||
describe("MemChecker resolve", () => {
|
||||
const checker = new MemChecker();
|
||||
|
||||
test("默认值:memory 为空对象", () => {
|
||||
const target: RawTargetConfig = { id: "mem-test", memory: {}, type: "memory" };
|
||||
test("默认值:mem 为空对象", () => {
|
||||
const target: RawTargetConfig = { id: "mem-test", mem: {}, type: "mem" };
|
||||
const resolved = checker.resolve(target, makeResolveContext());
|
||||
expect(resolved.memory).toEqual({});
|
||||
expect(resolved.mem).toEqual({});
|
||||
});
|
||||
|
||||
test("无 expect 时 expect 为 undefined", () => {
|
||||
const target: RawTargetConfig = { id: "mem-test", memory: {}, type: "memory" };
|
||||
const target: RawTargetConfig = { id: "mem-test", mem: {}, type: "mem" };
|
||||
const resolved = checker.resolve(target, makeResolveContext());
|
||||
expect(resolved.expect).toBeUndefined();
|
||||
});
|
||||
@@ -57,27 +57,27 @@ describe("MemoryChecker resolve", () => {
|
||||
const target: RawTargetConfig = {
|
||||
expect: { usagePercent: { lte: 85 } },
|
||||
id: "mem-test",
|
||||
memory: {},
|
||||
type: "memory",
|
||||
mem: {},
|
||||
type: "mem",
|
||||
};
|
||||
const resolved = checker.resolve(target, makeResolveContext());
|
||||
expect(resolved.expect).toEqual({ usagePercent: { lte: 85 } });
|
||||
});
|
||||
|
||||
test("type 为 memory", () => {
|
||||
const target: RawTargetConfig = { id: "mem-test", memory: {}, type: "memory" };
|
||||
test("type 为 mem", () => {
|
||||
const target: RawTargetConfig = { id: "mem-test", mem: {}, type: "mem" };
|
||||
const resolved = checker.resolve(target, makeResolveContext());
|
||||
expect(resolved.type).toBe("memory");
|
||||
expect(resolved.type).toBe("mem");
|
||||
});
|
||||
});
|
||||
|
||||
describe("MemoryChecker execute", () => {
|
||||
describe("MemChecker execute", () => {
|
||||
test("成功匹配", async () => {
|
||||
const data = makeMemData({ active: 4294967296, total: 8589934592 });
|
||||
const reader = () => Promise.resolve(data);
|
||||
const checker = new MemoryChecker(reader);
|
||||
const checker = new MemChecker(reader);
|
||||
|
||||
const target: RawTargetConfig = { id: "mem-test", memory: {}, type: "memory" };
|
||||
const target: RawTargetConfig = { id: "mem-test", mem: {}, type: "mem" };
|
||||
const resolved = checker.resolve(target, makeResolveContext());
|
||||
resolved.expect = { usagePercent: { lte: 85 } };
|
||||
|
||||
@@ -95,9 +95,9 @@ describe("MemoryChecker execute", () => {
|
||||
test("usagePercent mismatch", async () => {
|
||||
const data = makeMemData({ active: 7730941132, total: 8589934592 });
|
||||
const reader = () => Promise.resolve(data);
|
||||
const checker = new MemoryChecker(reader);
|
||||
const checker = new MemChecker(reader);
|
||||
|
||||
const target: RawTargetConfig = { id: "mem-test", memory: {}, type: "memory" };
|
||||
const target: RawTargetConfig = { id: "mem-test", mem: {}, type: "mem" };
|
||||
const resolved = checker.resolve(target, makeResolveContext());
|
||||
resolved.expect = { usagePercent: { lte: 50 } };
|
||||
|
||||
@@ -111,9 +111,9 @@ describe("MemoryChecker execute", () => {
|
||||
test("observation 包含所有字段", async () => {
|
||||
const data = makeMemData();
|
||||
const reader = () => Promise.resolve(data);
|
||||
const checker = new MemoryChecker(reader);
|
||||
const checker = new MemChecker(reader);
|
||||
|
||||
const target: RawTargetConfig = { id: "mem-test", memory: {}, type: "memory" };
|
||||
const target: RawTargetConfig = { id: "mem-test", mem: {}, type: "mem" };
|
||||
const resolved = checker.resolve(target, makeResolveContext());
|
||||
|
||||
const ctx = { signal: new AbortController().signal };
|
||||
@@ -139,25 +139,25 @@ describe("MemoryChecker execute", () => {
|
||||
|
||||
test("reader reject 返回失败结果", async () => {
|
||||
const reader = () => Promise.reject(new Error("read error"));
|
||||
const checker = new MemoryChecker(reader);
|
||||
const checker = new MemChecker(reader);
|
||||
|
||||
const target: RawTargetConfig = { id: "mem-test", memory: {}, type: "memory" };
|
||||
const target: RawTargetConfig = { id: "mem-test", mem: {}, type: "mem" };
|
||||
const resolved = checker.resolve(target, makeResolveContext());
|
||||
|
||||
const ctx = { signal: new AbortController().signal };
|
||||
const result = await checker.execute(resolved, ctx);
|
||||
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.failure?.phase).toBe("memory");
|
||||
expect(result.failure?.phase).toBe("mem");
|
||||
expect(result.failure?.path).toBe("snapshot");
|
||||
expect(result.observation).toBeNull();
|
||||
});
|
||||
|
||||
test("signal 已 abort 时返回 timeout failure", async () => {
|
||||
const reader = () => Promise.resolve(makeMemData());
|
||||
const checker = new MemoryChecker(reader);
|
||||
const checker = new MemChecker(reader);
|
||||
|
||||
const target: RawTargetConfig = { id: "mem-test", memory: {}, type: "memory" };
|
||||
const target: RawTargetConfig = { id: "mem-test", mem: {}, type: "mem" };
|
||||
const resolved = checker.resolve(target, makeResolveContext());
|
||||
|
||||
const controller = new AbortController();
|
||||
@@ -165,7 +165,7 @@ describe("MemoryChecker execute", () => {
|
||||
const result = await checker.execute(resolved, { signal: controller.signal });
|
||||
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.failure?.phase).toBe("memory");
|
||||
expect(result.failure?.phase).toBe("mem");
|
||||
expect(result.failure?.path).toBe("timeout");
|
||||
expect(result.observation).toBeNull();
|
||||
});
|
||||
@@ -175,9 +175,9 @@ describe("MemoryChecker execute", () => {
|
||||
new Promise<Systeminformation.MemData>(() => {
|
||||
// 故意永不 resolve,模拟悬挂的 reader
|
||||
});
|
||||
const checker = new MemoryChecker(reader);
|
||||
const checker = new MemChecker(reader);
|
||||
|
||||
const target: RawTargetConfig = { id: "mem-test", memory: {}, type: "memory" };
|
||||
const target: RawTargetConfig = { id: "mem-test", mem: {}, type: "mem" };
|
||||
const resolved = checker.resolve(target, makeResolveContext());
|
||||
|
||||
const controller = new AbortController();
|
||||
@@ -188,7 +188,7 @@ describe("MemoryChecker execute", () => {
|
||||
const result = await executePromise;
|
||||
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.failure?.phase).toBe("memory");
|
||||
expect(result.failure?.phase).toBe("mem");
|
||||
expect(result.failure?.path).toBe("timeout");
|
||||
expect(result.observation).toBeNull();
|
||||
});
|
||||
@@ -196,9 +196,9 @@ describe("MemoryChecker execute", () => {
|
||||
test("reader 在 abort 前 resolve 时返回正常结果", async () => {
|
||||
const data = makeMemData({ active: 4294967296, total: 8589934592 });
|
||||
const reader = () => Promise.resolve(data);
|
||||
const checker = new MemoryChecker(reader);
|
||||
const checker = new MemChecker(reader);
|
||||
|
||||
const target: RawTargetConfig = { id: "mem-test", memory: {}, type: "memory" };
|
||||
const target: RawTargetConfig = { id: "mem-test", mem: {}, type: "mem" };
|
||||
const resolved = checker.resolve(target, makeResolveContext());
|
||||
resolved.expect = { usagePercent: { lte: 85 } };
|
||||
|
||||
@@ -216,9 +216,9 @@ describe("MemoryChecker execute", () => {
|
||||
test("detail 格式", async () => {
|
||||
const data = makeMemData({ active: 4294967296, total: 8589934592 });
|
||||
const reader = () => Promise.resolve(data);
|
||||
const checker = new MemoryChecker(reader);
|
||||
const checker = new MemChecker(reader);
|
||||
|
||||
const target: RawTargetConfig = { id: "mem-test", memory: {}, type: "memory" };
|
||||
const target: RawTargetConfig = { id: "mem-test", mem: {}, type: "mem" };
|
||||
const resolved = checker.resolve(target, makeResolveContext());
|
||||
|
||||
const ctx = { signal: new AbortController().signal };
|
||||
@@ -231,13 +231,13 @@ describe("MemoryChecker execute", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("MemoryChecker serialize", () => {
|
||||
describe("MemChecker serialize", () => {
|
||||
test("序列化输出", () => {
|
||||
const checker = new MemoryChecker();
|
||||
const target: RawTargetConfig = { id: "mem-test", memory: {}, type: "memory" };
|
||||
const checker = new MemChecker();
|
||||
const target: RawTargetConfig = { id: "mem-test", mem: {}, type: "mem" };
|
||||
const resolved = checker.resolve(target, makeResolveContext());
|
||||
const result = checker.serialize(resolved);
|
||||
expect(result.target).toBe("memory");
|
||||
expect(result.target).toBe("mem");
|
||||
const config = JSON.parse(result.config) as Record<string, unknown>;
|
||||
expect(config).toEqual({});
|
||||
});
|
||||
@@ -16,9 +16,9 @@ import {
|
||||
checkUsagePercent,
|
||||
checkUsedBytes,
|
||||
checkUsedPercent,
|
||||
} from "../../../../../src/server/checker/runner/memory/expect";
|
||||
} from "../../../../../src/server/checker/runner/mem/expect";
|
||||
|
||||
describe("Memory expect checks - 百分比字段", () => {
|
||||
describe("Mem expect checks - 百分比字段", () => {
|
||||
test("checkUsagePercent 匹配", () => {
|
||||
expect(checkUsagePercent(50, { lte: 85 }).matched).toBe(true);
|
||||
});
|
||||
@@ -63,7 +63,7 @@ describe("Memory expect checks - 百分比字段", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("Memory expect checks - 字节字段", () => {
|
||||
describe("Mem expect checks - 字节字段", () => {
|
||||
test("checkActiveBytes 匹配", () => {
|
||||
expect(checkActiveBytes(4294967296, { lte: 8589934592 }).matched).toBe(true);
|
||||
});
|
||||
@@ -87,7 +87,7 @@ describe("Memory expect checks - 字节字段", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("Memory expect checks - swap 字段", () => {
|
||||
describe("Mem expect checks - swap 字段", () => {
|
||||
test("checkSwapUsagePercent null 通过 gte 检查 (Number(null)=0)", () => {
|
||||
expect(checkSwapUsagePercent(null, { gte: 0 }).matched).toBe(true);
|
||||
});
|
||||
@@ -132,7 +132,7 @@ describe("Memory expect checks - swap 字段", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("Memory expect checks - buffcacheBytes", () => {
|
||||
describe("Mem expect checks - buffcacheBytes", () => {
|
||||
test("checkBuffcacheBytes 有值时匹配", () => {
|
||||
expect(checkBuffcacheBytes(1073741824, { lte: 2147483648 }).matched).toBe(true);
|
||||
});
|
||||
@@ -1,50 +1,50 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
|
||||
import { normalizeTargetExpect } from "../../../../../src/server/checker/runner/memory/normalize";
|
||||
import { normalizeTargetExpect } from "../../../../../src/server/checker/runner/mem/normalize";
|
||||
|
||||
describe("normalizeTargetExpect (memory)", () => {
|
||||
describe("normalizeTargetExpect (mem)", () => {
|
||||
test("无 expect 直接返回", () => {
|
||||
const target = { id: "test", memory: {}, type: "memory" };
|
||||
const target = { id: "test", mem: {}, type: "mem" };
|
||||
expect(normalizeTargetExpect(target)).toEqual(target);
|
||||
});
|
||||
|
||||
test("expect 为非对象直接返回", () => {
|
||||
const target = { expect: "not-an-object", id: "test", memory: {}, type: "memory" };
|
||||
const target = { expect: "not-an-object", id: "test", mem: {}, type: "mem" };
|
||||
expect(normalizeTargetExpect(target)).toEqual(target);
|
||||
});
|
||||
|
||||
test("字节大小字符串 512MB 转换为数字", () => {
|
||||
const target = { expect: { usedBytes: "512MB" }, id: "test", memory: {}, type: "memory" };
|
||||
const target = { expect: { usedBytes: "512MB" }, id: "test", mem: {}, type: "mem" };
|
||||
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 target = { expect: { totalBytes: "1GB" }, id: "test", mem: {}, type: "mem" };
|
||||
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 target = { expect: { usedBytes: 1073741824 }, id: "test", mem: {}, type: "mem" };
|
||||
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 target = { expect: { usagePercent: 85 }, id: "test", mem: {}, type: "mem" };
|
||||
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 target = { expect: { usagePercent: { lte: 85 } }, id: "test", mem: {}, type: "mem" };
|
||||
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 target = { expect: { usedBytes: { gte: "512MB" } }, id: "test", mem: {}, type: "mem" };
|
||||
const result = normalizeTargetExpect(target);
|
||||
expect((result.expect as Record<string, unknown>)["usedBytes"]).toEqual({ gte: 536870912 });
|
||||
});
|
||||
@@ -53,8 +53,8 @@ describe("normalizeTargetExpect (memory)", () => {
|
||||
const target = {
|
||||
expect: { freePercent: 25, totalBytes: "16GB", usagePercent: { lte: 85 } },
|
||||
id: "test",
|
||||
memory: {},
|
||||
type: "memory",
|
||||
mem: {},
|
||||
type: "mem",
|
||||
};
|
||||
const result = normalizeTargetExpect(target);
|
||||
const expectObj = result.expect as Record<string, unknown>;
|
||||
@@ -64,9 +64,9 @@ describe("normalizeTargetExpect (memory)", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("normalizeTargetExpect (memory) 错误", () => {
|
||||
describe("normalizeTargetExpect (mem) 错误", () => {
|
||||
test("非法大小字符串抛出", () => {
|
||||
const target = { expect: { usedBytes: "abc" }, id: "test", memory: {}, type: "memory" };
|
||||
const target = { expect: { usedBytes: "abc" }, id: "test", mem: {}, type: "mem" };
|
||||
expect(() => normalizeTargetExpect(target)).toThrow();
|
||||
});
|
||||
});
|
||||
@@ -1,60 +1,60 @@
|
||||
import Ajv from "ajv";
|
||||
import { describe, expect, test } from "bun:test";
|
||||
|
||||
import { memoryCheckerSchemas } from "../../../../../src/server/checker/runner/memory/schema";
|
||||
import { memCheckerSchemas } from "../../../../../src/server/checker/runner/mem/schema";
|
||||
|
||||
const ajv = new Ajv({ strict: false });
|
||||
|
||||
describe("Memory checker schema", () => {
|
||||
describe("Mem checker schema", () => {
|
||||
test("authoring config 空配置通过", () => {
|
||||
const validate = ajv.compile(memoryCheckerSchemas.authoring.config);
|
||||
const validate = ajv.compile(memCheckerSchemas.authoring.config);
|
||||
expect(validate({})).toBe(true);
|
||||
});
|
||||
|
||||
test("normalized config 空配置通过", () => {
|
||||
const validate = ajv.compile(memoryCheckerSchemas.normalized.config);
|
||||
const validate = ajv.compile(memCheckerSchemas.normalized.config);
|
||||
expect(validate({})).toBe(true);
|
||||
});
|
||||
|
||||
test("config 拒绝额外字段", () => {
|
||||
const validate = ajv.compile(memoryCheckerSchemas.authoring.config);
|
||||
const validate = ajv.compile(memCheckerSchemas.authoring.config);
|
||||
expect(validate({ extraField: true })).toBe(false);
|
||||
});
|
||||
|
||||
test("authoring expect 允许百分比 ValueMatcher 简写", () => {
|
||||
const validate = ajv.compile(memoryCheckerSchemas.authoring.expect);
|
||||
const validate = ajv.compile(memCheckerSchemas.authoring.expect);
|
||||
expect(validate({ usagePercent: 85 })).toBe(true);
|
||||
expect(validate({ usagePercent: { lte: 85 } })).toBe(true);
|
||||
});
|
||||
|
||||
test("authoring expect 允许字节字段字符串", () => {
|
||||
const validate = ajv.compile(memoryCheckerSchemas.authoring.expect);
|
||||
const validate = ajv.compile(memCheckerSchemas.authoring.expect);
|
||||
expect(validate({ usedBytes: "512MB" })).toBe(true);
|
||||
expect(validate({ totalBytes: "1GB" })).toBe(true);
|
||||
});
|
||||
|
||||
test("authoring expect 允许字节字段数字", () => {
|
||||
const validate = ajv.compile(memoryCheckerSchemas.authoring.expect);
|
||||
const validate = ajv.compile(memCheckerSchemas.authoring.expect);
|
||||
expect(validate({ usedBytes: 536870912 })).toBe(true);
|
||||
});
|
||||
|
||||
test("normalized expect 允许 matcher 对象", () => {
|
||||
const validate = ajv.compile(memoryCheckerSchemas.normalized.expect);
|
||||
const validate = ajv.compile(memCheckerSchemas.normalized.expect);
|
||||
expect(validate({ freePercent: { gte: 15 }, usagePercent: { lte: 85 } })).toBe(true);
|
||||
});
|
||||
|
||||
test("expect 拒绝未知字段", () => {
|
||||
const validate = ajv.compile(memoryCheckerSchemas.authoring.expect);
|
||||
const validate = ajv.compile(memCheckerSchemas.authoring.expect);
|
||||
expect(validate({ unknownField: 1 })).toBe(false);
|
||||
});
|
||||
|
||||
test("expect 空对象通过", () => {
|
||||
const validate = ajv.compile(memoryCheckerSchemas.normalized.expect);
|
||||
const validate = ajv.compile(memCheckerSchemas.normalized.expect);
|
||||
expect(validate({})).toBe(true);
|
||||
});
|
||||
|
||||
test("expect 允许所有合法百分比字段", () => {
|
||||
const validate = ajv.compile(memoryCheckerSchemas.normalized.expect);
|
||||
const validate = ajv.compile(memCheckerSchemas.normalized.expect);
|
||||
expect(
|
||||
validate({
|
||||
activePercent: { lte: 80 },
|
||||
@@ -68,7 +68,7 @@ describe("Memory checker schema", () => {
|
||||
});
|
||||
|
||||
test("expect 允许所有合法字节字段", () => {
|
||||
const validate = ajv.compile(memoryCheckerSchemas.normalized.expect);
|
||||
const validate = ajv.compile(memCheckerSchemas.normalized.expect);
|
||||
expect(
|
||||
validate({
|
||||
activeBytes: { lte: 8589934592 },
|
||||
@@ -84,12 +84,12 @@ describe("Memory checker schema", () => {
|
||||
});
|
||||
|
||||
test("expect 允许 durationMs 字段", () => {
|
||||
const validate = ajv.compile(memoryCheckerSchemas.normalized.expect);
|
||||
const validate = ajv.compile(memCheckerSchemas.normalized.expect);
|
||||
expect(validate({ durationMs: { lte: 5000 } })).toBe(true);
|
||||
});
|
||||
|
||||
test("expect 允许 buffcacheBytes 字段", () => {
|
||||
const validate = ajv.compile(memoryCheckerSchemas.normalized.expect);
|
||||
const validate = ajv.compile(memCheckerSchemas.normalized.expect);
|
||||
expect(validate({ buffcacheBytes: { lte: 2147483648 } })).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -2,29 +2,29 @@ import { describe, expect, test } from "bun:test";
|
||||
|
||||
import type { RawTargetConfig } from "../../../../../src/server/checker/types";
|
||||
|
||||
import { validateMemoryConfig } from "../../../../../src/server/checker/runner/memory/validate";
|
||||
import { validateMemConfig } from "../../../../../src/server/checker/runner/mem/validate";
|
||||
|
||||
function validate(target: RawTargetConfig) {
|
||||
return validateMemoryConfig({ targets: [target] });
|
||||
return validateMemConfig({ targets: [target] });
|
||||
}
|
||||
|
||||
describe("validateMemoryConfig", () => {
|
||||
describe("validateMemConfig", () => {
|
||||
test("有效配置无错误", () => {
|
||||
expect(validate({ id: "mem-test", memory: {}, type: "memory" })).toEqual([]);
|
||||
expect(validate({ id: "mem-test", mem: {}, type: "mem" })).toEqual([]);
|
||||
});
|
||||
|
||||
test("缺少 memory 配置分组", () => {
|
||||
const issues = validate({ id: "mem-test", type: "memory" });
|
||||
expect(issues.some((i) => i.path.endsWith("memory") && i.code === "required")).toBe(true);
|
||||
test("缺少 mem 配置分组", () => {
|
||||
const issues = validate({ id: "mem-test", type: "mem" });
|
||||
expect(issues.some((i) => i.path.endsWith("mem") && i.code === "required")).toBe(true);
|
||||
});
|
||||
|
||||
test("memory 未知字段报错", () => {
|
||||
const issues = validate({ id: "mem-test", memory: { extra: true }, type: "memory" });
|
||||
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", memory: {}, type: "memory" });
|
||||
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);
|
||||
});
|
||||
|
||||
@@ -32,24 +32,24 @@ describe("validateMemoryConfig", () => {
|
||||
const issues = validate({
|
||||
expect: { usagePercent: { lte: 85 }, usedBytes: { lte: 8589934592 } },
|
||||
id: "mem-test",
|
||||
memory: {},
|
||||
type: "memory",
|
||||
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", memory: {}, type: "memory" });
|
||||
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", memory: {}, type: "memory" });
|
||||
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", memory: {}, type: "memory" });
|
||||
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);
|
||||
});
|
||||
|
||||
@@ -74,13 +74,13 @@ describe("validateMemoryConfig", () => {
|
||||
usedPercent: { lte: 90 },
|
||||
},
|
||||
id: "mem-test",
|
||||
memory: {},
|
||||
type: "memory",
|
||||
mem: {},
|
||||
type: "mem",
|
||||
});
|
||||
expect(issues).toEqual([]);
|
||||
});
|
||||
|
||||
test("非 memory type 的 target 不校验", () => {
|
||||
test("非 mem type 的 target 不校验", () => {
|
||||
const issues = validate({ id: "other-test", type: "http" });
|
||||
expect(issues).toEqual([]);
|
||||
});
|
||||
@@ -84,7 +84,7 @@ describe("CheckerRegistry", () => {
|
||||
"dns",
|
||||
"ws",
|
||||
"cpu",
|
||||
"memory",
|
||||
"mem",
|
||||
"custom",
|
||||
]);
|
||||
expect(second.supportedTypes).toEqual([
|
||||
@@ -98,7 +98,7 @@ describe("CheckerRegistry", () => {
|
||||
"dns",
|
||||
"ws",
|
||||
"cpu",
|
||||
"memory",
|
||||
"mem",
|
||||
]);
|
||||
expect(
|
||||
first.definitions.every((checker) => checker.schemas.authoring.config && checker.schemas.normalized.expect),
|
||||
|
||||
Reference in New Issue
Block a user