feat: 重构为多类型 checker 通用框架,支持 HTTP 与命令检查
- 引入 typed target 判别联合,支持 http 与 command 两种 checker - expect 重构为有序规则数组,按配置顺序快速失败并生成结构化 failure - 新增 command runner,支持 exec + args 本地命令执行 - 引入全局并发限制 maxConcurrentChecks 和 size 解析 (KB/MB/GB) - HTTP/command 各自独立 expect pipeline,应用领域默认成功语义 - SQLite schema、API、Dashboard 全链路调整为 checker 通用契约 - 补充完整测试覆盖(192 tests),更新 README 与示例配置
This commit is contained in:
287
tests/server/checker/expect/body.test.ts
Normal file
287
tests/server/checker/expect/body.test.ts
Normal file
@@ -0,0 +1,287 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import {
|
||||
applyOperator,
|
||||
checkBodyExpect,
|
||||
checkExpectValue,
|
||||
evaluateJsonPath,
|
||||
} from "../../../../src/server/checker/expect/body";
|
||||
|
||||
describe("evaluateJsonPath", () => {
|
||||
const obj = {
|
||||
status: "ok",
|
||||
code: 0,
|
||||
active: true,
|
||||
error: null,
|
||||
data: {
|
||||
count: 42,
|
||||
items: [{ name: "a" }, { name: "b" }],
|
||||
nested: { deep: "value" },
|
||||
},
|
||||
emptyObj: {},
|
||||
emptyArr: [],
|
||||
};
|
||||
|
||||
test("简单字段访问", () => {
|
||||
expect(evaluateJsonPath(obj, "$.status")).toBe("ok");
|
||||
expect(evaluateJsonPath(obj, "$.code")).toBe(0);
|
||||
expect(evaluateJsonPath(obj, "$.active")).toBe(true);
|
||||
expect(evaluateJsonPath(obj, "$.error")).toBeNull();
|
||||
});
|
||||
|
||||
test("嵌套对象访问", () => {
|
||||
expect(evaluateJsonPath(obj, "$.data.count")).toBe(42);
|
||||
expect(evaluateJsonPath(obj, "$.data.nested.deep")).toBe("value");
|
||||
});
|
||||
|
||||
test("数组索引访问", () => {
|
||||
expect(evaluateJsonPath(obj, "$.data.items[0].name")).toBe("a");
|
||||
expect(evaluateJsonPath(obj, "$.data.items[1].name")).toBe("b");
|
||||
});
|
||||
|
||||
test("路径不存在返回 undefined", () => {
|
||||
expect(evaluateJsonPath(obj, "$.notExist")).toBeUndefined();
|
||||
expect(evaluateJsonPath(obj, "$.data.notExist")).toBeUndefined();
|
||||
expect(evaluateJsonPath(obj, "$.data.items[99]")).toBeUndefined();
|
||||
});
|
||||
|
||||
test("空对象和空数组", () => {
|
||||
expect(evaluateJsonPath(obj, "$.emptyObj")).toEqual({});
|
||||
expect(evaluateJsonPath(obj, "$.emptyArr")).toEqual([]);
|
||||
});
|
||||
|
||||
test("非 $ 开头路径返回 undefined", () => {
|
||||
expect(evaluateJsonPath(obj, "status")).toBeUndefined();
|
||||
expect(evaluateJsonPath(obj, ".status")).toBeUndefined();
|
||||
});
|
||||
|
||||
test("null 对象上访问", () => {
|
||||
expect(evaluateJsonPath(null, "$.any")).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("applyOperator", () => {
|
||||
test("equals 操作符", () => {
|
||||
expect(applyOperator("ok", { equals: "ok" })).toBe(true);
|
||||
expect(applyOperator("ok", { equals: "error" })).toBe(false);
|
||||
expect(applyOperator(42, { equals: 42 })).toBe(true);
|
||||
expect(applyOperator(42, { equals: 41 })).toBe(false);
|
||||
expect(applyOperator(null, { equals: null })).toBe(true);
|
||||
expect(applyOperator(true, { equals: true })).toBe(true);
|
||||
});
|
||||
|
||||
test("contains 操作符", () => {
|
||||
expect(applyOperator("hello world", { contains: "hello" })).toBe(true);
|
||||
expect(applyOperator("hello world", { contains: "missing" })).toBe(false);
|
||||
expect(applyOperator(12345, { contains: "23" })).toBe(true);
|
||||
});
|
||||
|
||||
test("match 操作符", () => {
|
||||
expect(applyOperator("v2.1.0", { match: "\\d+\\.\\d+\\.\\d+" })).toBe(true);
|
||||
expect(applyOperator("v2.1", { match: "\\d+\\.\\d+\\.\\d+" })).toBe(false);
|
||||
expect(applyOperator("abc123", { match: "^\\w+\\d+$" })).toBe(true);
|
||||
});
|
||||
|
||||
test("empty 操作符", () => {
|
||||
expect(applyOperator("", { empty: true })).toBe(true);
|
||||
expect(applyOperator(null, { empty: true })).toBe(true);
|
||||
expect(applyOperator(undefined, { empty: true })).toBe(true);
|
||||
expect(applyOperator([], { empty: true })).toBe(true);
|
||||
expect(applyOperator({}, { empty: true })).toBe(true);
|
||||
expect(applyOperator("ok", { empty: true })).toBe(false);
|
||||
expect(applyOperator([1, 2], { empty: false })).toBe(true);
|
||||
expect(applyOperator([], { empty: false })).toBe(false);
|
||||
});
|
||||
|
||||
test("exists 操作符", () => {
|
||||
expect(applyOperator("ok", { exists: true })).toBe(true);
|
||||
expect(applyOperator(null, { exists: true })).toBe(true);
|
||||
expect(applyOperator(undefined, { exists: true })).toBe(false);
|
||||
expect(applyOperator(undefined, { exists: false })).toBe(true);
|
||||
expect(applyOperator("ok", { exists: false })).toBe(false);
|
||||
});
|
||||
|
||||
test("gte 操作符", () => {
|
||||
expect(applyOperator(10, { gte: 5 })).toBe(true);
|
||||
expect(applyOperator(5, { gte: 5 })).toBe(true);
|
||||
expect(applyOperator(3, { gte: 5 })).toBe(false);
|
||||
expect(applyOperator("10", { gte: 5 })).toBe(true);
|
||||
});
|
||||
|
||||
test("lte 操作符", () => {
|
||||
expect(applyOperator(3, { lte: 5 })).toBe(true);
|
||||
expect(applyOperator(5, { lte: 5 })).toBe(true);
|
||||
expect(applyOperator(10, { lte: 5 })).toBe(false);
|
||||
});
|
||||
|
||||
test("gt 操作符", () => {
|
||||
expect(applyOperator(10, { gt: 5 })).toBe(true);
|
||||
expect(applyOperator(5, { gt: 5 })).toBe(false);
|
||||
});
|
||||
|
||||
test("lt 操作符", () => {
|
||||
expect(applyOperator(3, { lt: 5 })).toBe(true);
|
||||
expect(applyOperator(5, { lt: 5 })).toBe(false);
|
||||
});
|
||||
|
||||
test("多操作符 AND 组合", () => {
|
||||
expect(applyOperator(7, { gte: 5, lte: 10 })).toBe(true);
|
||||
expect(applyOperator(3, { gte: 5, lte: 10 })).toBe(false);
|
||||
expect(applyOperator(15, { gte: 5, lte: 10 })).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("checkExpectValue", () => {
|
||||
test("原始值直接比较", () => {
|
||||
expect(checkExpectValue("ok", "ok")).toBe(true);
|
||||
expect(checkExpectValue("ok", "error")).toBe(false);
|
||||
expect(checkExpectValue(42, 42)).toBe(true);
|
||||
expect(checkExpectValue(null, null)).toBe(true);
|
||||
});
|
||||
|
||||
test("对象作为操作符", () => {
|
||||
expect(checkExpectValue(42, { gte: 10 })).toBe(true);
|
||||
expect(checkExpectValue(42, { gte: 100 })).toBe(false);
|
||||
expect(checkExpectValue("hello", { contains: "ell" })).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("checkBodyExpect (BodyRule[])", () => {
|
||||
test("无规则返回匹配成功", () => {
|
||||
const r = checkBodyExpect("anything");
|
||||
expect(r.matched).toBe(true);
|
||||
expect(r.failure).toBeNull();
|
||||
});
|
||||
|
||||
test("空规则数组返回匹配成功", () => {
|
||||
const r = checkBodyExpect("anything", []);
|
||||
expect(r.matched).toBe(true);
|
||||
expect(r.failure).toBeNull();
|
||||
});
|
||||
|
||||
test("contains 规则匹配成功", () => {
|
||||
const r = checkBodyExpect("hello world", [{ contains: "hello" }]);
|
||||
expect(r.matched).toBe(true);
|
||||
expect(r.failure).toBeNull();
|
||||
});
|
||||
|
||||
test("contains 规则匹配失败", () => {
|
||||
const r = checkBodyExpect("hello world", [{ contains: "missing" }]);
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure).not.toBeNull();
|
||||
expect(r.failure!.kind).toBe("mismatch");
|
||||
expect(r.failure!.phase).toBe("body");
|
||||
expect(r.failure!.path).toBe("body[0]");
|
||||
});
|
||||
|
||||
test("regex 规则匹配成功", () => {
|
||||
const r = checkBodyExpect("status: ok", [{ regex: "ok" }]);
|
||||
expect(r.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("regex 规则匹配失败", () => {
|
||||
const r = checkBodyExpect("status: error", [{ regex: "^ok$" }]);
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.path).toBe("body[0]");
|
||||
});
|
||||
|
||||
test("json 等值匹配成功", () => {
|
||||
const body = JSON.stringify({ status: "ok", code: 0 });
|
||||
const r = checkBodyExpect(body, [{ json: { path: "$.status", equals: "ok" } }]);
|
||||
expect(r.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("json 等值匹配失败", () => {
|
||||
const body = JSON.stringify({ status: "ok" });
|
||||
const r = checkBodyExpect(body, [{ json: { path: "$.status", equals: "error" } }]);
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.kind).toBe("mismatch");
|
||||
});
|
||||
|
||||
test("json 操作符匹配", () => {
|
||||
const body = JSON.stringify({ count: 42, version: "v2.1.0" });
|
||||
expect(checkBodyExpect(body, [{ json: { path: "$.count", gte: 10 } }]).matched).toBe(true);
|
||||
expect(checkBodyExpect(body, [{ json: { path: "$.version", match: "\\d+\\.\\d+\\.\\d+" } }]).matched).toBe(true);
|
||||
expect(checkBodyExpect(body, [{ json: { path: "$.count", gte: 100 } }]).matched).toBe(false);
|
||||
});
|
||||
|
||||
test("json 路径不存在", () => {
|
||||
const body = JSON.stringify({ status: "ok" });
|
||||
const r = checkBodyExpect(body, [{ json: { path: "$.notExist", equals: "value" } }]);
|
||||
expect(r.matched).toBe(false);
|
||||
});
|
||||
|
||||
test("json 解析失败", () => {
|
||||
const r = checkBodyExpect("not json", [{ json: { path: "$.status", equals: "ok" } }]);
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.kind).toBe("error");
|
||||
});
|
||||
|
||||
test("css 文本内容匹配", () => {
|
||||
const html = "<div id='health'>OK</div><span class='ver'>1.0</span>";
|
||||
expect(checkBodyExpect(html, [{ css: { selector: "div#health", equals: "OK" } }]).matched).toBe(true);
|
||||
expect(checkBodyExpect(html, [{ css: { selector: "span.ver", equals: "1.0" } }]).matched).toBe(true);
|
||||
expect(checkBodyExpect(html, [{ css: { selector: "div#health", equals: "ERROR" } }]).matched).toBe(false);
|
||||
});
|
||||
|
||||
test("css 选择器无匹配元素", () => {
|
||||
const html = "<div>OK</div>";
|
||||
const r = checkBodyExpect(html, [{ css: { selector: "span.missing", equals: "OK" } }]);
|
||||
expect(r.matched).toBe(false);
|
||||
});
|
||||
|
||||
test("css attr 提取", () => {
|
||||
const html = '<meta name="version" content="2.0.1">';
|
||||
expect(
|
||||
checkBodyExpect(html, [{ css: { selector: 'meta[name="version"]', attr: "content", equals: "2.0.1" } }]).matched,
|
||||
).toBe(true);
|
||||
expect(
|
||||
checkBodyExpect(html, [
|
||||
{ css: { selector: 'meta[name="version"]', attr: "content", match: "\\d+\\.\\d+\\.\\d+" } },
|
||||
]).matched,
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
test("css exists 检查", () => {
|
||||
const html = "<div id='test'>OK</div>";
|
||||
expect(checkBodyExpect(html, [{ css: { selector: "div#test", exists: true } }]).matched).toBe(true);
|
||||
expect(checkBodyExpect(html, [{ css: { selector: "span#missing", exists: false } }]).matched).toBe(true);
|
||||
expect(checkBodyExpect(html, [{ css: { selector: "div#test", exists: false } }]).matched).toBe(false);
|
||||
});
|
||||
|
||||
test("xpath 节点文本匹配", () => {
|
||||
const xml = "<root><status>ok</status><code>200</code></root>";
|
||||
expect(checkBodyExpect(xml, [{ xpath: { path: "/root/status/text()", equals: "ok" } }]).matched).toBe(true);
|
||||
expect(checkBodyExpect(xml, [{ xpath: { path: "/root/status/text()", equals: "error" } }]).matched).toBe(false);
|
||||
});
|
||||
|
||||
test("xpath 无匹配节点", () => {
|
||||
const xml = "<root><status>ok</status></root>";
|
||||
const r = checkBodyExpect(xml, [{ xpath: { path: "/root/missing/text()", equals: "ok" } }]);
|
||||
expect(r.matched).toBe(false);
|
||||
});
|
||||
|
||||
test("规则数组按顺序检查,第一条失败立即返回", () => {
|
||||
const body = JSON.stringify({ status: "error" });
|
||||
const r = checkBodyExpect(body, [{ contains: "healthy" }, { json: { path: "$.status", equals: "error" } }]);
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.path).toBe("body[0]");
|
||||
});
|
||||
|
||||
test("多条规则全部通过", () => {
|
||||
const body = JSON.stringify({ status: "healthy", count: 5 });
|
||||
const r = checkBodyExpect(body, [
|
||||
{ contains: "healthy" },
|
||||
{ json: { path: "$.status", equals: "healthy" } },
|
||||
{ json: { path: "$.count", gte: 1 } },
|
||||
]);
|
||||
expect(r.matched).toBe(true);
|
||||
expect(r.failure).toBeNull();
|
||||
});
|
||||
|
||||
test("第二条规则失败返回正确索引", () => {
|
||||
const body = JSON.stringify({ status: "ok" });
|
||||
const r = checkBodyExpect(body, [{ contains: "ok" }, { json: { path: "$.status", equals: "error" } }]);
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.path).toContain("body[1]");
|
||||
});
|
||||
});
|
||||
168
tests/server/checker/expect/command.test.ts
Normal file
168
tests/server/checker/expect/command.test.ts
Normal file
@@ -0,0 +1,168 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { checkCommandExpect } from "../../../../src/server/checker/expect/command";
|
||||
import type { CommandObservation } from "../../../../src/server/checker/expect/command";
|
||||
import type { CommandExpectConfig } from "../../../../src/server/checker/types";
|
||||
|
||||
function obs(overrides: Partial<CommandObservation> = {}): CommandObservation {
|
||||
return {
|
||||
exitCode: 0,
|
||||
stdout: "",
|
||||
stderr: "",
|
||||
durationMs: 100,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
describe("checkCommandExpect", () => {
|
||||
test("无 expect 配置时默认检查 exitCode [0] 匹配成功", () => {
|
||||
const r = checkCommandExpect(obs());
|
||||
expect(r.matched).toBe(true);
|
||||
expect(r.failure).toBeNull();
|
||||
});
|
||||
|
||||
test("无 expect 配置时 exitCode 非 0 匹配失败", () => {
|
||||
const r = checkCommandExpect(obs({ exitCode: 1 }));
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure).not.toBeNull();
|
||||
expect(r.failure!.phase).toBe("exitCode");
|
||||
expect(r.failure!.kind).toBe("mismatch");
|
||||
});
|
||||
|
||||
test("exitCode 匹配指定退出码", () => {
|
||||
const cfg: CommandExpectConfig = { exitCode: [0, 1] };
|
||||
expect(checkCommandExpect(obs({ exitCode: 0 }), cfg).matched).toBe(true);
|
||||
expect(checkCommandExpect(obs({ exitCode: 1 }), cfg).matched).toBe(true);
|
||||
expect(checkCommandExpect(obs({ exitCode: 2 }), cfg).matched).toBe(false);
|
||||
});
|
||||
|
||||
test("exitCode 不匹配返回 phase=exitCode 的失败", () => {
|
||||
const r = checkCommandExpect(obs({ exitCode: 2 }), { exitCode: [0] });
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.phase).toBe("exitCode");
|
||||
expect(r.failure!.expected).toEqual([0]);
|
||||
expect(r.failure!.actual).toBe(2);
|
||||
});
|
||||
|
||||
test("duration 在限制内匹配成功", () => {
|
||||
const r = checkCommandExpect(obs({ durationMs: 50 }), { maxDurationMs: 100 });
|
||||
expect(r.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("duration 超过限制匹配失败", () => {
|
||||
const r = checkCommandExpect(obs({ durationMs: 200 }), { maxDurationMs: 100 });
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.phase).toBe("duration");
|
||||
});
|
||||
|
||||
test("stdout TextRule 数组匹配", () => {
|
||||
const o = obs({ stdout: "build completed successfully" });
|
||||
expect(checkCommandExpect(o, { stdout: [{ contains: "completed" }] }).matched).toBe(true);
|
||||
expect(checkCommandExpect(o, { stdout: [{ contains: "failed" }] }).matched).toBe(false);
|
||||
expect(checkCommandExpect(o, { stdout: [{ match: "completed.*successfully$" }] }).matched).toBe(true);
|
||||
});
|
||||
|
||||
test("stdout 多条规则全部通过", () => {
|
||||
const o = obs({ stdout: "version: 3.2.1, build: ok" });
|
||||
const r = checkCommandExpect(o, {
|
||||
stdout: [{ contains: "version" }, { match: "\\d+\\.\\d+\\.\\d+" }],
|
||||
});
|
||||
expect(r.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("stdout 第一条规则失败立即返回", () => {
|
||||
const o = obs({ stdout: "error occurred" });
|
||||
const r = checkCommandExpect(o, {
|
||||
stdout: [{ contains: "success" }, { contains: "error" }],
|
||||
});
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.phase).toBe("stdout");
|
||||
expect(r.failure!.path).toBe("stdout[0]");
|
||||
});
|
||||
|
||||
test("stderr TextRule 数组匹配", () => {
|
||||
const o = obs({ stderr: "warning: deprecated" });
|
||||
expect(checkCommandExpect(o, { stderr: [{ contains: "warning" }] }).matched).toBe(true);
|
||||
expect(checkCommandExpect(o, { stderr: [{ contains: "error" }] }).matched).toBe(false);
|
||||
});
|
||||
|
||||
test("stdout 失败阻止 stderr 检查", () => {
|
||||
const o = obs({ stdout: "bad output", stderr: "warning message" });
|
||||
const r = checkCommandExpect(o, {
|
||||
exitCode: [0],
|
||||
stdout: [{ contains: "success" }],
|
||||
stderr: [{ contains: "warning" }],
|
||||
});
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.phase).toBe("stdout");
|
||||
});
|
||||
|
||||
test("stdout 通过但 stderr 失败", () => {
|
||||
const o = obs({ stdout: "ok", stderr: "fatal error" });
|
||||
const r = checkCommandExpect(o, {
|
||||
stdout: [{ contains: "ok" }],
|
||||
stderr: [{ equals: "clean" }],
|
||||
});
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.phase).toBe("stderr");
|
||||
});
|
||||
|
||||
test("完整流水线 exitCode->duration->stdout->stderr 全部通过", () => {
|
||||
const o = obs({
|
||||
exitCode: 0,
|
||||
durationMs: 50,
|
||||
stdout: "build success",
|
||||
stderr: "",
|
||||
});
|
||||
const r = checkCommandExpect(o, {
|
||||
exitCode: [0],
|
||||
maxDurationMs: 100,
|
||||
stdout: [{ contains: "success" }],
|
||||
stderr: [{ empty: true }],
|
||||
});
|
||||
expect(r.matched).toBe(true);
|
||||
expect(r.failure).toBeNull();
|
||||
});
|
||||
|
||||
test("完整流水线 exitCode 通过但 duration 失败", () => {
|
||||
const o = obs({ exitCode: 0, durationMs: 500 });
|
||||
const r = checkCommandExpect(o, {
|
||||
exitCode: [0],
|
||||
maxDurationMs: 100,
|
||||
stdout: [{ contains: "ok" }],
|
||||
});
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.phase).toBe("duration");
|
||||
});
|
||||
|
||||
test("完整流水线 exitCode/duration 通过但 stdout 失败", () => {
|
||||
const o = obs({ exitCode: 0, durationMs: 50, stdout: "error" });
|
||||
const r = checkCommandExpect(o, {
|
||||
exitCode: [0],
|
||||
maxDurationMs: 100,
|
||||
stdout: [{ contains: "success" }],
|
||||
});
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.phase).toBe("stdout");
|
||||
});
|
||||
|
||||
test("完整流水线 exitCode/duration/stdout 通过但 stderr 失败", () => {
|
||||
const o = obs({ exitCode: 0, durationMs: 50, stdout: "ok", stderr: "warning" });
|
||||
const r = checkCommandExpect(o, {
|
||||
exitCode: [0],
|
||||
maxDurationMs: 100,
|
||||
stdout: [{ contains: "ok" }],
|
||||
stderr: [{ empty: true }],
|
||||
});
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.phase).toBe("stderr");
|
||||
});
|
||||
|
||||
test("stdout 操作符组合", () => {
|
||||
const o = obs({ stdout: "count: 42" });
|
||||
expect(
|
||||
checkCommandExpect(o, {
|
||||
stdout: [{ contains: "count" }, { match: "\\d+" }],
|
||||
}).matched,
|
||||
).toBe(true);
|
||||
});
|
||||
});
|
||||
79
tests/server/checker/expect/failure.test.ts
Normal file
79
tests/server/checker/expect/failure.test.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { truncateActual, mismatchFailure, errorFailure } from "../../../../src/server/checker/expect/failure";
|
||||
|
||||
describe("truncateActual", () => {
|
||||
test("短字符串不截断", () => {
|
||||
expect(truncateActual("hello")).toBe("hello");
|
||||
});
|
||||
|
||||
test("恰好等于限制长度不截断", () => {
|
||||
const str = "a".repeat(200);
|
||||
expect(truncateActual(str)).toBe(str);
|
||||
});
|
||||
|
||||
test("超过限制长度截断并加省略号", () => {
|
||||
const str = "a".repeat(300);
|
||||
const result = truncateActual(str) as string;
|
||||
expect(result.length).toBe(203);
|
||||
expect(result.endsWith("...")).toBe(true);
|
||||
expect(result.startsWith("a".repeat(200))).toBe(true);
|
||||
});
|
||||
|
||||
test("自定义最大长度", () => {
|
||||
const str = "abcdefghij";
|
||||
const result = truncateActual(str, 5) as string;
|
||||
expect(result).toBe("abcde...");
|
||||
});
|
||||
|
||||
test("null 不截断", () => {
|
||||
expect(truncateActual(null)).toBe(null);
|
||||
});
|
||||
|
||||
test("undefined 不截断", () => {
|
||||
expect(truncateActual(undefined)).toBe(undefined);
|
||||
});
|
||||
|
||||
test("数字转换为字符串后判断", () => {
|
||||
expect(truncateActual(42)).toBe(42);
|
||||
expect(truncateActual(123456789, 3) as string).toBe("123...");
|
||||
});
|
||||
});
|
||||
|
||||
describe("mismatchFailure", () => {
|
||||
test("返回正确的 mismatch 结构", () => {
|
||||
const f = mismatchFailure("status", "status", [200], 500, "status mismatch");
|
||||
expect(f).toEqual({
|
||||
kind: "mismatch",
|
||||
phase: "status",
|
||||
path: "status",
|
||||
expected: [200],
|
||||
actual: 500,
|
||||
message: "status mismatch",
|
||||
});
|
||||
});
|
||||
|
||||
test("自动截断过长的 actual", () => {
|
||||
const longStr = "x".repeat(300);
|
||||
const f = mismatchFailure("body", "body[0]", "short", longStr, "too long");
|
||||
expect((f.actual as string).endsWith("...")).toBe(true);
|
||||
expect((f.actual as string).length).toBe(203);
|
||||
});
|
||||
});
|
||||
|
||||
describe("errorFailure", () => {
|
||||
test("返回正确的 error 结构", () => {
|
||||
const f = errorFailure("body", "body[0].json($.x)", "body is not valid JSON");
|
||||
expect(f).toEqual({
|
||||
kind: "error",
|
||||
phase: "body",
|
||||
path: "body[0].json($.x)",
|
||||
message: "body is not valid JSON",
|
||||
});
|
||||
});
|
||||
|
||||
test("不含 expected 和 actual 字段", () => {
|
||||
const f = errorFailure("headers", "headers.x", "header missing");
|
||||
expect(f).not.toHaveProperty("expected");
|
||||
expect(f).not.toHaveProperty("actual");
|
||||
});
|
||||
});
|
||||
165
tests/server/checker/expect/http.test.ts
Normal file
165
tests/server/checker/expect/http.test.ts
Normal file
@@ -0,0 +1,165 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { checkHttpExpect } from "../../../../src/server/checker/expect/http";
|
||||
import type { HttpObservation } from "../../../../src/server/checker/expect/http";
|
||||
import type { HttpExpectConfig } from "../../../../src/server/checker/types";
|
||||
|
||||
function obs(overrides: Partial<HttpObservation> = {}): HttpObservation {
|
||||
return {
|
||||
statusCode: 200,
|
||||
headers: {},
|
||||
body: "",
|
||||
durationMs: 100,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
describe("checkHttpExpect", () => {
|
||||
test("无 expect 配置时默认检查 status [200] 匹配成功", () => {
|
||||
const r = checkHttpExpect(obs());
|
||||
expect(r.matched).toBe(true);
|
||||
expect(r.failure).toBeNull();
|
||||
});
|
||||
|
||||
test("无 expect 配置时 status 非 200 匹配失败", () => {
|
||||
const r = checkHttpExpect(obs({ statusCode: 500 }));
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure).not.toBeNull();
|
||||
expect(r.failure!.phase).toBe("status");
|
||||
expect(r.failure!.kind).toBe("mismatch");
|
||||
});
|
||||
|
||||
test("status 匹配指定状态码", () => {
|
||||
const cfg: HttpExpectConfig = { status: [200, 301] };
|
||||
expect(checkHttpExpect(obs({ statusCode: 200 }), cfg).matched).toBe(true);
|
||||
expect(checkHttpExpect(obs({ statusCode: 301 }), cfg).matched).toBe(true);
|
||||
expect(checkHttpExpect(obs({ statusCode: 404 }), cfg).matched).toBe(false);
|
||||
});
|
||||
|
||||
test("status 不匹配返回 phase=status 的失败", () => {
|
||||
const r = checkHttpExpect(obs({ statusCode: 503 }), { status: [200] });
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.phase).toBe("status");
|
||||
expect(r.failure!.expected).toEqual([200]);
|
||||
expect(r.failure!.actual).toBe(503);
|
||||
});
|
||||
|
||||
test("duration 在限制内匹配成功", () => {
|
||||
const r = checkHttpExpect(obs({ durationMs: 50 }), { maxDurationMs: 100 });
|
||||
expect(r.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("duration 超过限制匹配失败", () => {
|
||||
const r = checkHttpExpect(obs({ durationMs: 200 }), { maxDurationMs: 100 });
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.phase).toBe("duration");
|
||||
});
|
||||
|
||||
test("duration 恰好等于限制匹配成功", () => {
|
||||
const r = checkHttpExpect(obs({ durationMs: 100 }), { maxDurationMs: 100 });
|
||||
expect(r.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("headers 字符串格式检查(等于)", () => {
|
||||
const o = obs({ headers: { "content-type": "application/json", "x-api": "v1" } });
|
||||
expect(checkHttpExpect(o, { headers: { "content-type": "application/json" } }).matched).toBe(true);
|
||||
expect(checkHttpExpect(o, { headers: { "content-type": "text/html" } }).matched).toBe(false);
|
||||
});
|
||||
|
||||
test("headers 操作符格式检查", () => {
|
||||
const o = obs({ headers: { "content-type": "application/json" } });
|
||||
expect(checkHttpExpect(o, { headers: { "content-type": { contains: "json" } } }).matched).toBe(true);
|
||||
expect(checkHttpExpect(o, { headers: { "content-type": { match: "^application/" } } }).matched).toBe(true);
|
||||
expect(checkHttpExpect(o, { headers: { "content-type": { contains: "xml" } } }).matched).toBe(false);
|
||||
});
|
||||
|
||||
test("headers 大小写不敏感匹配", () => {
|
||||
const o = obs({ headers: { "content-type": "application/json" } });
|
||||
expect(checkHttpExpect(o, { headers: { "Content-Type": "application/json" } }).matched).toBe(true);
|
||||
});
|
||||
|
||||
test("headers 不存在时返回失败", () => {
|
||||
const o = obs({ headers: {} });
|
||||
const r = checkHttpExpect(o, { headers: { "x-missing": "value" } });
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.phase).toBe("headers");
|
||||
});
|
||||
|
||||
test("body 规则数组按顺序检查", () => {
|
||||
const o = obs({ body: JSON.stringify({ status: "ok", count: 5 }) });
|
||||
const r = checkHttpExpect(o, {
|
||||
body: [{ contains: "ok" }, { json: { path: "$.count", gte: 1 } }],
|
||||
});
|
||||
expect(r.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("body 第一条规则失败立即返回", () => {
|
||||
const o = obs({ body: "hello world" });
|
||||
const r = checkHttpExpect(o, {
|
||||
body: [{ contains: "missing" }, { contains: "hello" }],
|
||||
});
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.path).toBe("body[0]");
|
||||
});
|
||||
|
||||
test("body 为 null 但有 body 规则时报错", () => {
|
||||
const o = obs({ body: null });
|
||||
const r = checkHttpExpect(o, { body: [{ contains: "test" }] });
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.kind).toBe("error");
|
||||
});
|
||||
|
||||
test("完整流水线 status->duration->headers->body 全部通过", () => {
|
||||
const o = obs({
|
||||
statusCode: 200,
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ status: "healthy" }),
|
||||
durationMs: 50,
|
||||
});
|
||||
const r = checkHttpExpect(o, {
|
||||
status: [200],
|
||||
maxDurationMs: 100,
|
||||
headers: { "content-type": { contains: "json" } },
|
||||
body: [{ json: { path: "$.status", equals: "healthy" } }],
|
||||
});
|
||||
expect(r.matched).toBe(true);
|
||||
expect(r.failure).toBeNull();
|
||||
});
|
||||
|
||||
test("完整流水线 status 通过但 duration 失败", () => {
|
||||
const o = obs({ statusCode: 200, durationMs: 500 });
|
||||
const r = checkHttpExpect(o, {
|
||||
status: [200],
|
||||
maxDurationMs: 100,
|
||||
});
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.phase).toBe("duration");
|
||||
});
|
||||
|
||||
test("完整流水线 status 和 duration 通过但 headers 失败", () => {
|
||||
const o = obs({ statusCode: 200, durationMs: 50, headers: { "x-api": "v1" } });
|
||||
const r = checkHttpExpect(o, {
|
||||
status: [200],
|
||||
maxDurationMs: 100,
|
||||
headers: { "x-api": "v2" },
|
||||
});
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.phase).toBe("headers");
|
||||
});
|
||||
|
||||
test("完整流水线 status/duration/headers 通过但 body 失败", () => {
|
||||
const o = obs({
|
||||
statusCode: 200,
|
||||
durationMs: 50,
|
||||
headers: { "content-type": "text/plain" },
|
||||
body: "error occurred",
|
||||
});
|
||||
const r = checkHttpExpect(o, {
|
||||
status: [200],
|
||||
maxDurationMs: 100,
|
||||
headers: { "content-type": "text/plain" },
|
||||
body: [{ contains: "success" }],
|
||||
});
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.phase).toBe("body");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user