import { describe, expect, test } from "bun:test"; import { checkBodyExpect } from "../../../../../src/server/checker/runner/shared/body"; 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({ code: 0, status: "ok" }); const r = checkBodyExpect(body, [{ json: { equals: "ok", path: "$.status" } }]); expect(r.matched).toBe(true); }); test("json 等值匹配失败", () => { const body = JSON.stringify({ status: "ok" }); const r = checkBodyExpect(body, [{ json: { equals: "error", path: "$.status" } }]); 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: { gte: 10, path: "$.count" } }]).matched).toBe(true); expect(checkBodyExpect(body, [{ json: { match: "\\d+\\.\\d+\\.\\d+", path: "$.version" } }]).matched).toBe(true); expect(checkBodyExpect(body, [{ json: { gte: 100, path: "$.count" } }]).matched).toBe(false); }); test("json 路径不存在", () => { const body = JSON.stringify({ status: "ok" }); const r = checkBodyExpect(body, [{ json: { equals: "value", path: "$.notExist" } }]); expect(r.matched).toBe(false); }); test("json 解析失败", () => { const r = checkBodyExpect("not json", [{ json: { equals: "ok", path: "$.status" } }]); expect(r.matched).toBe(false); expect(r.failure!.kind).toBe("error"); }); test("css 文本内容匹配", () => { const html = "
200