From ec35d6b5ba80ea87a154ecb4cf697b9729a832d5 Mon Sep 17 00:00:00 2001 From: zengLingbiao Date: Sat, 18 Jul 2026 08:09:02 +0800 Subject: [PATCH] test(shared): add unit tests for agent liveness state helpers (#98546) --- src/shared/agent-liveness.test.ts | 106 ++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 src/shared/agent-liveness.test.ts diff --git a/src/shared/agent-liveness.test.ts b/src/shared/agent-liveness.test.ts new file mode 100644 index 000000000000..38d19be88603 --- /dev/null +++ b/src/shared/agent-liveness.test.ts @@ -0,0 +1,106 @@ +// Agent liveness tests cover blocked-run state detection and error formatting. +import { describe, expect, it } from "vitest"; +import { + isBlockedLivenessState, + formatBlockedLivenessError, + normalizeBlockedLivenessWaitStatus, +} from "./agent-liveness.js"; + +describe("isBlockedLivenessState", () => { + it("returns true for blocked in any casing or with surrounding whitespace", () => { + expect(isBlockedLivenessState("blocked")).toBe(true); + expect(isBlockedLivenessState("BLOCKED")).toBe(true); + expect(isBlockedLivenessState(" Blocked ")).toBe(true); + }); + + it("returns false for non-blocked or non-string values", () => { + expect(isBlockedLivenessState("ok")).toBe(false); + expect(isBlockedLivenessState("")).toBe(false); + expect(isBlockedLivenessState(undefined)).toBe(false); + expect(isBlockedLivenessState(null)).toBe(false); + expect(isBlockedLivenessState(123)).toBe(false); + }); +}); + +describe("formatBlockedLivenessError", () => { + it("returns the trimmed error message when given a string", () => { + expect(formatBlockedLivenessError("timeout")).toBe("timeout"); + expect(formatBlockedLivenessError(" connection lost ")).toBe("connection lost"); + }); + + it("returns a default message for empty or non-string values", () => { + expect(formatBlockedLivenessError("")).toBe( + "Agent run blocked before producing a usable result.", + ); + expect(formatBlockedLivenessError(undefined)).toBe( + "Agent run blocked before producing a usable result.", + ); + expect(formatBlockedLivenessError(null)).toBe( + "Agent run blocked before producing a usable result.", + ); + expect(formatBlockedLivenessError(123)).toBe( + "Agent run blocked before producing a usable result.", + ); + }); +}); + +describe("normalizeBlockedLivenessWaitStatus", () => { + it("converts status to error when liveness state is blocked", () => { + expect( + normalizeBlockedLivenessWaitStatus({ + status: "ok", + livenessState: "blocked", + }), + ).toEqual({ + status: "error", + error: "Agent run blocked before producing a usable result.", + }); + }); + + it("preserves the original status when liveness is not blocked", () => { + expect( + normalizeBlockedLivenessWaitStatus({ + status: "ok", + livenessState: undefined, + }), + ).toEqual({ status: "ok" }); + expect( + normalizeBlockedLivenessWaitStatus({ + status: "timeout", + livenessState: "running", + }), + ).toEqual({ status: "timeout" }); + }); + + it("passes through error string when liveness is not blocked", () => { + expect( + normalizeBlockedLivenessWaitStatus({ + status: "timeout", + error: "request timed out", + }), + ).toEqual({ status: "timeout", error: "request timed out" }); + }); + + it("uses provided error message in blocked state", () => { + expect( + normalizeBlockedLivenessWaitStatus({ + status: "pending", + livenessState: "blocked", + error: "gateway unavailable", + }), + ).toEqual({ status: "error", error: "gateway unavailable" }); + }); + + it("falls back to default message when blocked with non-string error", () => { + expect( + normalizeBlockedLivenessWaitStatus({ + status: "error", + livenessState: "blocked", + error: 500, + }), + ).toEqual({ + status: "error", + error: "Agent run blocked before producing a usable result.", + }); + }); +});