From 5aa46e0517d4a13b661ee86701e2a0288f708f08 Mon Sep 17 00:00:00 2001 From: daxiong Date: Thu, 26 Feb 2026 10:22:34 +0800 Subject: [PATCH] test(acp): add unit tests for meta.ts helper functions Add comprehensive test coverage for readString, readBool, and readNumber functions in src/acp/meta.ts: - readString: tests for finding first non-empty string, trimming, handling null/undefined meta, skipping non-string values - readBool: tests for finding boolean values, handling false vs undefined, null/undefined meta cases - readNumber: tests for finding finite numbers, rejecting Infinity/NaN, handling negative numbers and zero These utility functions are used throughout the ACP (Agent Communication Protocol) layer for reading typed values from metadata objects. Having test coverage ensures they handle edge cases correctly. --- src/acp/meta.test.ts | 80 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 src/acp/meta.test.ts diff --git a/src/acp/meta.test.ts b/src/acp/meta.test.ts new file mode 100644 index 00000000000..26cb81d3147 --- /dev/null +++ b/src/acp/meta.test.ts @@ -0,0 +1,80 @@ +import { describe, expect, it } from "vitest"; +import { readString, readBool, readNumber } from "./meta.js"; + +describe("readString", () => { + it("returns the first non-empty string value", () => { + const meta = { a: "", b: " ", c: "value", d: "other" }; + expect(readString(meta, ["a", "b", "c", "d"])).toBe("value"); + }); + + it("returns undefined when no keys match", () => { + const meta = { a: "", b: " ", c: null }; + expect(readString(meta, ["a", "b", "c", "d"])).toBeUndefined(); + }); + + it("returns undefined for null/undefined meta", () => { + expect(readString(null, ["a"])).toBeUndefined(); + expect(readString(undefined, ["a"])).toBeUndefined(); + }); + + it("trims whitespace from values", () => { + const meta = { a: " trimmed " }; + expect(readString(meta, ["a"])).toBe("trimmed"); + }); + + it("skips non-string values", () => { + const meta = { a: 123, b: true, c: "string" }; + expect(readString(meta, ["a", "b", "c"])).toBe("string"); + }); +}); + +describe("readBool", () => { + it("returns the first boolean value", () => { + const meta = { a: "string", b: true, c: false }; + expect(readBool(meta, ["a", "b", "c"])).toBe(true); + }); + + it("returns undefined when no boolean found", () => { + const meta = { a: "true", b: 1, c: null }; + expect(readBool(meta, ["a", "b", "c"])).toBeUndefined(); + }); + + it("returns false when explicitly set to false", () => { + const meta = { a: false }; + expect(readBool(meta, ["a"])).toBe(false); + }); + + it("returns undefined for null/undefined meta", () => { + expect(readBool(null, ["a"])).toBeUndefined(); + expect(readBool(undefined, ["a"])).toBeUndefined(); + }); +}); + +describe("readNumber", () => { + it("returns the first finite number value", () => { + const meta = { a: "string", b: 42, c: 100 }; + expect(readNumber(meta, ["a", "b", "c"])).toBe(42); + }); + + it("returns undefined when no number found", () => { + const meta = { a: "123", b: true, c: null }; + expect(readNumber(meta, ["a", "b", "c"])).toBeUndefined(); + }); + + it("returns undefined for non-finite numbers", () => { + const meta = { a: Infinity, b: NaN, c: 42 }; + expect(readNumber(meta, ["a", "b", "c"])).toBe(42); + expect(readNumber({ a: Infinity }, ["a"])).toBeUndefined(); + expect(readNumber({ a: NaN }, ["a"])).toBeUndefined(); + }); + + it("returns undefined for null/undefined meta", () => { + expect(readNumber(null, ["a"])).toBeUndefined(); + expect(readNumber(undefined, ["a"])).toBeUndefined(); + }); + + it("handles negative numbers and zero", () => { + expect(readNumber({ a: -5 }, ["a"])).toBe(-5); + expect(readNumber({ a: 0 }, ["a"])).toBe(0); + }); +});