refactor: 移除 success 字段,简化为 matched 单层判定模型
This commit is contained in:
@@ -27,7 +27,6 @@ function makeTarget(
|
||||
describe("runCommandCheck", () => {
|
||||
test("exitCode=0 成功", async () => {
|
||||
const result = await runCommandCheck(makeTarget({ exec: "true", args: [] }));
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.matched).toBe(true);
|
||||
expect(result.statusDetail).toBe("exitCode=0");
|
||||
expect(result.failure).toBeNull();
|
||||
@@ -35,7 +34,6 @@ describe("runCommandCheck", () => {
|
||||
|
||||
test("exitCode=1 不匹配默认 [0]", async () => {
|
||||
const result = await runCommandCheck(makeTarget({ exec: "false", args: [] }));
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.statusDetail).toBe("exitCode=1");
|
||||
expect(result.failure).not.toBeNull();
|
||||
@@ -44,14 +42,13 @@ describe("runCommandCheck", () => {
|
||||
|
||||
test("exitCode=1 匹配自定义 [1]", async () => {
|
||||
const result = await runCommandCheck(makeTarget({ exec: "false", args: [] }, { expect: { exitCode: [1] } }));
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.matched).toBe(true);
|
||||
expect(result.statusDetail).toBe("exitCode=1");
|
||||
});
|
||||
|
||||
test("命令不存在返回 spawn 错误", async () => {
|
||||
const result = await runCommandCheck(makeTarget({ exec: "/nonexistent/command/xyz" }));
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.failure).not.toBeNull();
|
||||
expect(result.failure!.phase).toBe("exitCode");
|
||||
expect(result.failure!.message).toBeTruthy();
|
||||
@@ -59,21 +56,20 @@ describe("runCommandCheck", () => {
|
||||
|
||||
test("超时返回错误", async () => {
|
||||
const result = await runCommandCheck(makeTarget({ exec: "sleep", args: ["10"] }, { timeoutMs: 100 }));
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.failure).not.toBeNull();
|
||||
expect(result.failure!.message).toContain("超时");
|
||||
});
|
||||
|
||||
test("stdout 输出捕获", async () => {
|
||||
const result = await runCommandCheck(makeTarget({ exec: "echo", args: ["hello world"] }));
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("stdout 匹配 expect", async () => {
|
||||
const result = await runCommandCheck(
|
||||
makeTarget({ exec: "echo", args: ["hello"] }, { expect: { stdout: [{ contains: "hello" }] } }),
|
||||
);
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.matched).toBe(true);
|
||||
});
|
||||
|
||||
@@ -81,7 +77,7 @@ describe("runCommandCheck", () => {
|
||||
const result = await runCommandCheck(
|
||||
makeTarget({ exec: "echo", args: ["hello"] }, { expect: { stdout: [{ contains: "nonexistent" }] } }),
|
||||
);
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.failure!.phase).toBe("stdout");
|
||||
});
|
||||
|
||||
@@ -89,7 +85,6 @@ describe("runCommandCheck", () => {
|
||||
const result = await runCommandCheck(
|
||||
makeTarget({ exec: "bash", args: ["-c", "echo error >&2"] }, { expect: { stderr: [{ contains: "error" }] } }),
|
||||
);
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.matched).toBe(true);
|
||||
});
|
||||
|
||||
@@ -101,7 +96,7 @@ describe("runCommandCheck", () => {
|
||||
maxOutputBytes: 10,
|
||||
}),
|
||||
);
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.failure).not.toBeNull();
|
||||
expect(result.failure!.message).toContain("超过限制");
|
||||
});
|
||||
@@ -114,7 +109,7 @@ describe("runCommandCheck", () => {
|
||||
|
||||
test("ls 命令执行成功", async () => {
|
||||
const result = await runCommandCheck(makeTarget({ exec: "ls", args: ["/tmp"] }));
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.matched).toBe(true);
|
||||
expect(result.statusDetail).toBe("exitCode=0");
|
||||
});
|
||||
|
||||
@@ -122,11 +117,11 @@ describe("runCommandCheck", () => {
|
||||
const result = await runCommandCheck(
|
||||
makeTarget({ exec: "echo", args: ["*"] }, { expect: { stdout: [{ contains: "*" }] } }),
|
||||
);
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("不提供 stdin,等待输入的命令会阻塞超时", async () => {
|
||||
const result = await runCommandCheck(makeTarget({ exec: "bash", args: ["-c", "read line"] }, { timeoutMs: 500 }));
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.matched).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user