1
0
Files
DiAL/tests/server/checker/runner/mem/normalize.test.ts
lanyuanxiaoyao 2f8fd8bd9c refactor: 将 memory checker 重命名为 mem
- 类型标识符 memory → mem
- 类名 MemoryChecker → MemChecker
- 内部类型名统一 Memory* → Mem*
- 内部函数名统一 *Memory* → *Mem*
- 目录重命名 memory/ → mem/(源码、测试、文档)
- 配置键 memory: → mem:
- 重新生成 probe-config.schema.json
- 保留中文"内存"用户提示

破坏性变更:无向后兼容
2026-05-27 18:19:29 +08:00

73 lines
3.1 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import { normalizeTargetExpect } from "../../../../../src/server/checker/runner/mem/normalize";
describe("normalizeTargetExpect (mem)", () => {
test("无 expect 直接返回", () => {
const target = { id: "test", mem: {}, type: "mem" };
expect(normalizeTargetExpect(target)).toEqual(target);
});
test("expect 为非对象直接返回", () => {
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", 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", 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", 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", 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", 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", mem: {}, type: "mem" };
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",
mem: {},
type: "mem",
};
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 (mem) 错误", () => {
test("非法大小字符串抛出", () => {
const target = { expect: { usedBytes: "abc" }, id: "test", mem: {}, type: "mem" };
expect(() => normalizeTargetExpect(target)).toThrow();
});
});