refactor: expect 类型模型重构,Raw/Resolved 双层分离与断言基础设施内聚
- 重命名 ContentRules→ContentExpectations, KeyValueExpect→KeyedExpectations - 新增 Raw/Resolved 双层模型:resolve 阶段物化为执行计划,store 持久化 Raw 快照 - HTTP body 按需读取:status/headers 失败或无 body expectation 时不读取 body - 新增 displayValueExpectation() 解包 failure.expected 用户可读展示 - 修复 checkEarlyTimeout 独立 lte/lt 检查,修复 KeyedExpectations JSON Schema - 新增 expect/value.ts(resolve/check/display)、keyed.ts、content.ts、headers.ts、status.ts - 删除旧 normalize.ts/matcher.ts/validate-matcher.ts/key-value.ts - 更新 DEVELOPMENT.md:expect 五层管线表、displayValueExpectation、1.7↔1.10 交叉引用 - 同步 13 个 main specs,归档 refactor-expect-type-model 变更(62/62 tasks)
This commit is contained in:
@@ -1,6 +1,14 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
|
||||
import { checkHeaders, checkStatus } from "../../../../../src/server/checker/runner/http/expect";
|
||||
import type { RawKeyedExpectations } from "../../../../../src/server/checker/expect/types";
|
||||
|
||||
import { checkHeaderExpectations } from "../../../../../src/server/checker/expect/headers";
|
||||
import { resolveKeyedExpectations } from "../../../../../src/server/checker/expect/keyed";
|
||||
import { checkStatusCode } from "../../../../../src/server/checker/expect/status";
|
||||
|
||||
function checkHeaders(headers: Record<string, unknown>, raw?: RawKeyedExpectations) {
|
||||
return checkHeaderExpectations(headers, resolveKeyedExpectations(raw));
|
||||
}
|
||||
|
||||
describe("checkHeaders", () => {
|
||||
test("未配置 headers expect 时匹配成功", () => {
|
||||
@@ -46,15 +54,15 @@ describe("checkHeaders", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("checkStatus 范围匹配", () => {
|
||||
test("无 expect 配置时默认 status [200] 可由调用方使用 checkStatus 表达", () => {
|
||||
const result = checkStatus(200, [200]);
|
||||
describe("checkStatusCode 范围匹配", () => {
|
||||
test("无 expect 配置时默认 status [200] 可由调用方使用 checkStatusCode 表达", () => {
|
||||
const result = checkStatusCode(200, [200]);
|
||||
expect(result.matched).toBe(true);
|
||||
expect(result.failure).toBeNull();
|
||||
});
|
||||
|
||||
test("status 不匹配返回 phase=status 的失败", () => {
|
||||
const result = checkStatus(503, [200]);
|
||||
const result = checkStatusCode(503, [200]);
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.failure!.phase).toBe("status");
|
||||
expect(result.failure!.expected).toEqual([200]);
|
||||
@@ -62,47 +70,47 @@ describe("checkStatus 范围匹配", () => {
|
||||
});
|
||||
|
||||
test("2xx 范围匹配 200", () => {
|
||||
expect(checkStatus(200, ["2xx"]).matched).toBe(true);
|
||||
expect(checkStatusCode(200, ["2xx"]).matched).toBe(true);
|
||||
});
|
||||
|
||||
test("2xx 范围匹配 204", () => {
|
||||
expect(checkStatus(204, ["2xx"]).matched).toBe(true);
|
||||
expect(checkStatusCode(204, ["2xx"]).matched).toBe(true);
|
||||
});
|
||||
|
||||
test("2xx 范围不匹配 301", () => {
|
||||
expect(checkStatus(301, ["2xx"]).matched).toBe(false);
|
||||
expect(checkStatusCode(301, ["2xx"]).matched).toBe(false);
|
||||
});
|
||||
|
||||
test("5xx 范围匹配 503", () => {
|
||||
expect(checkStatus(503, ["5xx"]).matched).toBe(true);
|
||||
expect(checkStatusCode(503, ["5xx"]).matched).toBe(true);
|
||||
});
|
||||
|
||||
test("混合精确值与范围模式命中精确值", () => {
|
||||
expect(checkStatus(301, ["2xx", 301]).matched).toBe(true);
|
||||
expect(checkStatusCode(301, ["2xx", 301]).matched).toBe(true);
|
||||
});
|
||||
|
||||
test("混合精确值与范围模式命中范围", () => {
|
||||
expect(checkStatus(204, ["2xx", 301]).matched).toBe(true);
|
||||
expect(checkStatusCode(204, ["2xx", 301]).matched).toBe(true);
|
||||
});
|
||||
|
||||
test("混合模式都不匹配", () => {
|
||||
expect(checkStatus(404, ["2xx", 301]).matched).toBe(false);
|
||||
expect(checkStatusCode(404, ["2xx", 301]).matched).toBe(false);
|
||||
});
|
||||
|
||||
test("纯精确值仍正常工作", () => {
|
||||
expect(checkStatus(200, [200, 201]).matched).toBe(true);
|
||||
expect(checkStatus(404, [200, 201]).matched).toBe(false);
|
||||
expect(checkStatusCode(200, [200, 201]).matched).toBe(true);
|
||||
expect(checkStatusCode(404, [200, 201]).matched).toBe(false);
|
||||
});
|
||||
|
||||
test("1xx 范围匹配 101", () => {
|
||||
expect(checkStatus(101, ["1xx"]).matched).toBe(true);
|
||||
expect(checkStatusCode(101, ["1xx"]).matched).toBe(true);
|
||||
});
|
||||
|
||||
test("3xx 范围匹配 301", () => {
|
||||
expect(checkStatus(301, ["3xx"]).matched).toBe(true);
|
||||
expect(checkStatusCode(301, ["3xx"]).matched).toBe(true);
|
||||
});
|
||||
|
||||
test("4xx 范围匹配 404", () => {
|
||||
expect(checkStatus(404, ["4xx"]).matched).toBe(true);
|
||||
expect(checkStatusCode(404, ["4xx"]).matched).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,13 +1,22 @@
|
||||
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
|
||||
|
||||
import type { ResolvedHttpTarget } from "../../../../../src/server/checker/runner/http/types";
|
||||
import type {
|
||||
RawHttpExpectConfig,
|
||||
ResolvedHttpExpectConfig,
|
||||
ResolvedHttpTarget,
|
||||
} from "../../../../../src/server/checker/runner/http/types";
|
||||
import type { CheckerContext, ResolveContext } from "../../../../../src/server/checker/runner/types";
|
||||
|
||||
import { resolveContentExpectations } from "../../../../../src/server/checker/expect/content";
|
||||
import { resolveKeyedExpectations } from "../../../../../src/server/checker/expect/keyed";
|
||||
import { checkStatusCode } from "../../../../../src/server/checker/expect/status";
|
||||
import { resolveValueExpectation } from "../../../../../src/server/checker/expect/value";
|
||||
import { HttpChecker } from "../../../../../src/server/checker/runner/http/execute";
|
||||
import { checkStatus } from "../../../../../src/server/checker/runner/http/expect";
|
||||
import { formatConfigIssues } from "../../../../../src/server/checker/schema/issues";
|
||||
|
||||
const checker = new HttpChecker();
|
||||
const SLOW_BODY_DELAY_MS = 1000;
|
||||
const FAST_RESPONSE_LIMIT_MS = 500;
|
||||
|
||||
function validateHttpTarget(target: unknown): string {
|
||||
return formatConfigIssues(checker.validate({ defaults: {}, targets: [target as never] }));
|
||||
@@ -145,7 +154,7 @@ describe("HttpChecker", () => {
|
||||
|
||||
function makeTarget(overrides: {
|
||||
body?: string;
|
||||
expect?: Record<string, unknown>;
|
||||
expect?: RawHttpExpectConfig;
|
||||
headers?: Record<string, string>;
|
||||
ignoreSSL?: boolean;
|
||||
maxBodyBytes?: number;
|
||||
@@ -154,9 +163,19 @@ describe("HttpChecker", () => {
|
||||
timeoutMs?: number;
|
||||
url?: string;
|
||||
}): ResolvedHttpTarget {
|
||||
const raw = overrides.expect;
|
||||
const resolvedExpect: ResolvedHttpExpectConfig | undefined = raw
|
||||
? {
|
||||
body: resolveContentExpectations(raw.body),
|
||||
durationMs: resolveValueExpectation(raw.durationMs),
|
||||
headers: resolveKeyedExpectations(raw.headers),
|
||||
status: raw.status ?? [200],
|
||||
}
|
||||
: undefined;
|
||||
|
||||
return {
|
||||
description: null,
|
||||
expect: overrides.expect,
|
||||
expect: resolvedExpect,
|
||||
group: "default",
|
||||
http: {
|
||||
body: overrides.body,
|
||||
@@ -170,6 +189,7 @@ describe("HttpChecker", () => {
|
||||
id: "test-http",
|
||||
intervalMs: 60000,
|
||||
name: "test-http",
|
||||
rawExpect: raw,
|
||||
timeoutMs: overrides.timeoutMs ?? 5000,
|
||||
type: "http",
|
||||
};
|
||||
@@ -181,6 +201,29 @@ describe("HttpChecker", () => {
|
||||
return { signal: controller.signal };
|
||||
}
|
||||
|
||||
function startSlowBodyServer(init: ResponseInit = {}) {
|
||||
return Bun.serve({
|
||||
fetch() {
|
||||
let sentFirstChunk = false;
|
||||
return new Response(
|
||||
new ReadableStream<Uint8Array>({
|
||||
async pull(controller) {
|
||||
if (!sentFirstChunk) {
|
||||
sentFirstChunk = true;
|
||||
controller.enqueue(new TextEncoder().encode("slow body"));
|
||||
return;
|
||||
}
|
||||
await new Promise((resolve) => setTimeout(resolve, SLOW_BODY_DELAY_MS));
|
||||
controller.close();
|
||||
},
|
||||
}),
|
||||
init,
|
||||
);
|
||||
},
|
||||
port: 0,
|
||||
});
|
||||
}
|
||||
|
||||
test("成功请求 200", async () => {
|
||||
const result = await checker.execute(makeTarget({ url: `${baseUrl}/ok` }), makeCtx());
|
||||
expect(result.matched).toBe(true);
|
||||
@@ -192,7 +235,7 @@ describe("HttpChecker", () => {
|
||||
test("404 不匹配默认 status [200]", async () => {
|
||||
const result = await checker.execute(makeTarget({ url: `${baseUrl}/notfound` }), makeCtx());
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.observation).toMatchObject({ bodyPreview: "not found", statusCode: 404 });
|
||||
expect(result.observation).toMatchObject({ bodyPreview: null, statusCode: 404 });
|
||||
expect(result.failure!.phase).toBe("status");
|
||||
});
|
||||
|
||||
@@ -218,7 +261,7 @@ describe("HttpChecker", () => {
|
||||
makeCtx(),
|
||||
);
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.observation).toMatchObject({ bodyPreview: "hello world", statusCode: 200 });
|
||||
expect(result.observation).toMatchObject({ bodyPreview: null, statusCode: 200 });
|
||||
expect(result.failure!.phase).toBe("headers");
|
||||
});
|
||||
|
||||
@@ -279,12 +322,43 @@ describe("HttpChecker", () => {
|
||||
});
|
||||
|
||||
test("快速失败:status 失败时不读取 body", async () => {
|
||||
const result = await checker.execute(
|
||||
makeTarget({ expect: { body: [{ contains: "something" }], status: [200] }, url: `${baseUrl}/notfound` }),
|
||||
makeCtx(),
|
||||
);
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.failure!.phase).toBe("status");
|
||||
const slowBodyServer = startSlowBodyServer({ status: 404 });
|
||||
try {
|
||||
const start = performance.now();
|
||||
const result = await checker.execute(
|
||||
makeTarget({
|
||||
expect: { body: [{ contains: "something" }], status: [200] },
|
||||
url: `http://localhost:${slowBodyServer.port}/`,
|
||||
}),
|
||||
makeCtx(),
|
||||
);
|
||||
expect(performance.now() - start).toBeLessThan(FAST_RESPONSE_LIMIT_MS);
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.failure!.phase).toBe("status");
|
||||
expect(result.observation?.["bodyPreview"]).toBeNull();
|
||||
} finally {
|
||||
void slowBodyServer.stop();
|
||||
}
|
||||
});
|
||||
|
||||
test("快速失败:headers 失败时不读取 body", async () => {
|
||||
const slowBodyServer = startSlowBodyServer({ headers: { "x-custom": "actual" }, status: 200 });
|
||||
try {
|
||||
const start = performance.now();
|
||||
const result = await checker.execute(
|
||||
makeTarget({
|
||||
expect: { body: [{ contains: "something" }], headers: { "x-custom": "expected" }, status: [200] },
|
||||
url: `http://localhost:${slowBodyServer.port}/`,
|
||||
}),
|
||||
makeCtx(),
|
||||
);
|
||||
expect(performance.now() - start).toBeLessThan(FAST_RESPONSE_LIMIT_MS);
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.failure!.phase).toBe("headers");
|
||||
expect(result.observation?.["bodyPreview"]).toBeNull();
|
||||
} finally {
|
||||
void slowBodyServer.stop();
|
||||
}
|
||||
});
|
||||
|
||||
test("status 通过但 body 失败", async () => {
|
||||
@@ -511,12 +585,40 @@ describe("HttpChecker", () => {
|
||||
}
|
||||
});
|
||||
|
||||
test("无 body rules 时不读取 body", async () => {
|
||||
const result = await checker.execute(
|
||||
makeTarget({ expect: { status: [200] }, maxBodyBytes: 1, url: `${baseUrl}/large` }),
|
||||
makeCtx(),
|
||||
);
|
||||
expect(result.matched).toBe(true);
|
||||
test("无 body expectations 时不读取 body", async () => {
|
||||
const slowBodyServer = startSlowBodyServer({ status: 200 });
|
||||
try {
|
||||
const start = performance.now();
|
||||
const result = await checker.execute(
|
||||
makeTarget({ expect: { status: [200] }, maxBodyBytes: 1, url: `http://localhost:${slowBodyServer.port}/` }),
|
||||
makeCtx(),
|
||||
);
|
||||
expect(performance.now() - start).toBeLessThan(FAST_RESPONSE_LIMIT_MS);
|
||||
expect(result.matched).toBe(true);
|
||||
expect(result.observation?.["bodyPreview"]).toBeNull();
|
||||
} finally {
|
||||
void slowBodyServer.stop();
|
||||
}
|
||||
});
|
||||
|
||||
test("进入 body 前 durationMs 上界已失败时不读取 body", async () => {
|
||||
const slowBodyServer = startSlowBodyServer({ status: 200 });
|
||||
try {
|
||||
const start = performance.now();
|
||||
const result = await checker.execute(
|
||||
makeTarget({
|
||||
expect: { body: [{ contains: "slow" }], durationMs: { lte: 0 }, status: [200] },
|
||||
url: `http://localhost:${slowBodyServer.port}/`,
|
||||
}),
|
||||
makeCtx(),
|
||||
);
|
||||
expect(performance.now() - start).toBeLessThan(FAST_RESPONSE_LIMIT_MS);
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.failure!.phase).toBe("duration");
|
||||
expect(result.observation?.["bodyPreview"]).toBeNull();
|
||||
} finally {
|
||||
void slowBodyServer.stop();
|
||||
}
|
||||
});
|
||||
|
||||
test("body 失败优先于 duration 检查", async () => {
|
||||
@@ -598,7 +700,7 @@ describe("HttpChecker", () => {
|
||||
expect(result.observation).toMatchObject({ statusCode: 200 });
|
||||
});
|
||||
|
||||
test("混合 body rules 集成检查", async () => {
|
||||
test("混合 body expectations 集成检查", async () => {
|
||||
const result = await checker.execute(
|
||||
makeTarget({
|
||||
expect: {
|
||||
@@ -649,7 +751,7 @@ describe("HttpChecker", () => {
|
||||
});
|
||||
|
||||
test("1xx 范围模式匹配 101", () => {
|
||||
const r = checkStatus(101, ["1xx"]);
|
||||
const r = checkStatusCode(101, ["1xx"]);
|
||||
expect(r.matched).toBe(true);
|
||||
});
|
||||
|
||||
@@ -868,6 +970,16 @@ describe("HttpChecker.resolve", () => {
|
||||
expect(result.http.ignoreSSL).toBe(false);
|
||||
});
|
||||
|
||||
test("未配置 expect 时在 Resolved 模型物化默认 status", () => {
|
||||
const result = checker.resolve(
|
||||
{ http: { url: "https://example.com" }, id: "test", name: "test", type: "http" },
|
||||
makeResolveContext(),
|
||||
);
|
||||
|
||||
expect(result.rawExpect).toBeUndefined();
|
||||
expect(result.expect).toEqual({ status: [200] });
|
||||
});
|
||||
|
||||
test("maxRedirects 默认值为 0", () => {
|
||||
const result = checker.resolve(
|
||||
{ http: { url: "https://example.com" }, id: "test", name: "test", type: "http" },
|
||||
|
||||
Reference in New Issue
Block a user