150 lines
4.7 KiB
TypeScript
150 lines
4.7 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
|
|
import {
|
|
checkActiveBytes,
|
|
checkActivePercent,
|
|
checkAvailableBytes,
|
|
checkAvailablePercent,
|
|
checkBuffcacheBytes,
|
|
checkFreeBytes,
|
|
checkFreePercent,
|
|
checkSwapFreeBytes,
|
|
checkSwapTotalBytes,
|
|
checkSwapUsagePercent,
|
|
checkSwapUsedBytes,
|
|
checkTotalBytes,
|
|
checkUsagePercent,
|
|
checkUsedBytes,
|
|
checkUsedPercent,
|
|
} from "../../../../../src/server/checker/runner/memory/expect";
|
|
|
|
describe("Memory expect checks - 百分比字段", () => {
|
|
test("checkUsagePercent 匹配", () => {
|
|
expect(checkUsagePercent(50, { lte: 85 }).matched).toBe(true);
|
|
});
|
|
|
|
test("checkUsagePercent 不匹配", () => {
|
|
const result = checkUsagePercent(90, { lte: 85 });
|
|
expect(result.matched).toBe(false);
|
|
expect(result.failure?.phase).toBe("usage");
|
|
});
|
|
|
|
test("checkUsedPercent 匹配", () => {
|
|
expect(checkUsedPercent(75, { lte: 80 }).matched).toBe(true);
|
|
});
|
|
|
|
test("checkUsedPercent 不匹配", () => {
|
|
const result = checkUsedPercent(85, { lte: 80 });
|
|
expect(result.matched).toBe(false);
|
|
expect(result.failure?.phase).toBe("used");
|
|
});
|
|
|
|
test("checkFreePercent 匹配", () => {
|
|
expect(checkFreePercent(25, { gte: 15 }).matched).toBe(true);
|
|
});
|
|
|
|
test("checkFreePercent 不匹配", () => {
|
|
const result = checkFreePercent(10, { gte: 15 });
|
|
expect(result.matched).toBe(false);
|
|
expect(result.failure?.phase).toBe("free");
|
|
});
|
|
|
|
test("checkActivePercent 匹配", () => {
|
|
expect(checkActivePercent(50, { lte: 85 }).matched).toBe(true);
|
|
});
|
|
|
|
test("checkAvailablePercent 匹配", () => {
|
|
expect(checkAvailablePercent(50, { gte: 20 }).matched).toBe(true);
|
|
});
|
|
|
|
test("undefined matcher 直接通过", () => {
|
|
expect(checkUsagePercent(99.9, undefined).matched).toBe(true);
|
|
expect(checkFreePercent(0, undefined).matched).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe("Memory expect checks - 字节字段", () => {
|
|
test("checkActiveBytes 匹配", () => {
|
|
expect(checkActiveBytes(4294967296, { lte: 8589934592 }).matched).toBe(true);
|
|
});
|
|
|
|
test("checkUsedBytes 不匹配", () => {
|
|
const result = checkUsedBytes(10737418240, { lte: 8589934592 });
|
|
expect(result.matched).toBe(false);
|
|
expect(result.failure?.phase).toBe("usedBytes");
|
|
});
|
|
|
|
test("checkFreeBytes 匹配", () => {
|
|
expect(checkFreeBytes(4294967296, { gte: 2147483648 }).matched).toBe(true);
|
|
});
|
|
|
|
test("checkAvailableBytes 匹配", () => {
|
|
expect(checkAvailableBytes(6442450944, { gte: 4294967296 }).matched).toBe(true);
|
|
});
|
|
|
|
test("checkTotalBytes 匹配", () => {
|
|
expect(checkTotalBytes(17179869184, { gte: 8589934592 }).matched).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe("Memory expect checks - swap 字段", () => {
|
|
test("checkSwapUsagePercent null 通过 gte 检查 (Number(null)=0)", () => {
|
|
expect(checkSwapUsagePercent(null, { gte: 0 }).matched).toBe(true);
|
|
});
|
|
|
|
test("checkSwapUsagePercent null 不匹配大于 0 的 gte", () => {
|
|
const result = checkSwapUsagePercent(null, { gte: 1 });
|
|
expect(result.matched).toBe(false);
|
|
expect(result.failure?.phase).toBe("swapUsage");
|
|
});
|
|
|
|
test("checkSwapUsagePercent 有值时正常匹配", () => {
|
|
expect(checkSwapUsagePercent(50, { lte: 80 }).matched).toBe(true);
|
|
});
|
|
|
|
test("checkSwapUsagePercent 有值时不匹配", () => {
|
|
const result = checkSwapUsagePercent(90, { lte: 80 });
|
|
expect(result.matched).toBe(false);
|
|
});
|
|
|
|
test("checkSwapUsedBytes null 通过 gte:0 (Number(null)=0)", () => {
|
|
expect(checkSwapUsedBytes(null, { gte: 0 }).matched).toBe(true);
|
|
});
|
|
|
|
test("checkSwapUsedBytes 0 通过 gte:0", () => {
|
|
expect(checkSwapUsedBytes(0, { gte: 0 }).matched).toBe(true);
|
|
});
|
|
|
|
test("checkSwapFreeBytes null 通过 gte:0", () => {
|
|
expect(checkSwapFreeBytes(null, { gte: 0 }).matched).toBe(true);
|
|
});
|
|
|
|
test("checkSwapTotalBytes null 匹配 equals:null", () => {
|
|
expect(checkSwapTotalBytes(null, { equals: null }).matched).toBe(true);
|
|
});
|
|
|
|
test("checkSwapTotalBytes 0 匹配 equals:0", () => {
|
|
expect(checkSwapTotalBytes(0, { equals: 0 }).matched).toBe(true);
|
|
});
|
|
|
|
test("checkSwapTotalBytes null 不匹配 equals:0", () => {
|
|
expect(checkSwapTotalBytes(null, { equals: 0 }).matched).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("Memory expect checks - buffcacheBytes", () => {
|
|
test("checkBuffcacheBytes 有值时匹配", () => {
|
|
expect(checkBuffcacheBytes(1073741824, { lte: 2147483648 }).matched).toBe(true);
|
|
});
|
|
|
|
test("checkBuffcacheBytes null 通过 gte:0 (Number(null)=0)", () => {
|
|
expect(checkBuffcacheBytes(null, { gte: 0 }).matched).toBe(true);
|
|
});
|
|
|
|
test("checkBuffcacheBytes null 不匹配 gte:1", () => {
|
|
const result = checkBuffcacheBytes(null, { gte: 1 });
|
|
expect(result.matched).toBe(false);
|
|
expect(result.failure?.phase).toBe("buffcacheBytes");
|
|
});
|
|
});
|