import Ajv from "ajv"; import { describe, expect, test } from "bun:test"; import { cpuCheckerSchemas } from "../../../../../src/server/checker/runner/cpu/schema"; const ajv = new Ajv({ strict: false }); describe("CPU checker schema", () => { test("authoring config 允许变量引用", () => { const validate = ajv.compile(cpuCheckerSchemas.authoring.config); expect(validate({ includePerCore: "${per_core|false}", sampleDuration: "${sample_dur|1s}" })).toBe(true); }); test("normalized config 允许合法值", () => { const validate = ajv.compile(cpuCheckerSchemas.normalized.config); expect(validate({ includePerCore: true, sampleDuration: "1s" })).toBe(true); }); test("normalized config 空配置通过", () => { const validate = ajv.compile(cpuCheckerSchemas.normalized.config); expect(validate({})).toBe(true); }); test("config 拒绝额外字段", () => { const validate = ajv.compile(cpuCheckerSchemas.authoring.config); expect(validate({ extraField: true })).toBe(false); }); test("authoring expect 允许 ValueMatcher 简写", () => { const validate = ajv.compile(cpuCheckerSchemas.authoring.expect); expect(validate({ usagePercent: 85 })).toBe(true); expect(validate({ usagePercent: { lte: 85 } })).toBe(true); }); test("normalized expect 允许 matcher 对象", () => { const validate = ajv.compile(cpuCheckerSchemas.normalized.expect); expect(validate({ idlePercent: { gte: 15 }, usagePercent: { lte: 85 } })).toBe(true); }); test("expect 拒绝 logicalCoreCount 字段", () => { const validate = ajv.compile(cpuCheckerSchemas.authoring.expect); expect(validate({ logicalCoreCount: { gte: 4 } })).toBe(false); }); test("expect 拒绝 userPercent 字段", () => { const validate = ajv.compile(cpuCheckerSchemas.authoring.expect); expect(validate({ userPercent: { lte: 50 } })).toBe(false); }); test("expect 拒绝 systemPercent 字段", () => { const validate = ajv.compile(cpuCheckerSchemas.authoring.expect); expect(validate({ systemPercent: { lte: 50 } })).toBe(false); }); test("expect 允许所有合法字段", () => { const validate = ajv.compile(cpuCheckerSchemas.normalized.expect); expect( validate({ durationMs: { lte: 2000 }, idlePercent: { gte: 15 }, maxCoreUsagePercent: { lte: 95 }, minCoreUsagePercent: { gte: 0 }, usagePercent: { lte: 85 }, }), ).toBe(true); }); test("expect 拒绝额外字段", () => { const validate = ajv.compile(cpuCheckerSchemas.normalized.expect); expect(validate({ unknownField: 1 })).toBe(false); }); test("expect 空对象通过", () => { const validate = ajv.compile(cpuCheckerSchemas.normalized.expect); expect(validate({})).toBe(true); }); });