From 4ec15ced190dbde861f557b0bc9b882eb514b7e5 Mon Sep 17 00:00:00 2001 From: FiniteSkills Date: Sat, 4 Jul 2026 07:06:55 +0530 Subject: [PATCH] test(lib): cover formatter safety edge cases --- src/lib/__tests__/format.test.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/lib/__tests__/format.test.ts b/src/lib/__tests__/format.test.ts index ed99ddb..597d36a 100644 --- a/src/lib/__tests__/format.test.ts +++ b/src/lib/__tests__/format.test.ts @@ -76,6 +76,10 @@ describe("safeStringify", () => { expect(result).toContain("[BigInt:10]"); }); + it("replaces bigint values inside arrays", () => { + expect(safeStringify([BigInt(7)])).toContain("[BigInt:7]"); + }); + it("replaces functions and undefined leaves with safe markers", () => { const result = safeStringify({ fn: () => undefined, missing: undefined }); expect(result).toContain("[Function]"); @@ -97,6 +101,18 @@ describe("safeStringify", () => { expect(result.endsWith(EVENT_PAYLOAD_TRUNCATED_MARKER.trim())).toBe(false); }); + it("respects a custom maxChars value when truncating", () => { + const result = safeStringify({ data: "abcdef" }, 10); + expect(result.length).toBe(10 + EVENT_PAYLOAD_TRUNCATED_MARKER.length); + expect(result).toContain(EVENT_PAYLOAD_TRUNCATED_MARKER.trim()); + }); + + it("does not truncate a payload exactly at the limit", () => { + const payload = { status: "ok" }; + const serialised = JSON.stringify(payload, null, 2); + expect(safeStringify(payload, serialised.length)).toBe(serialised); + }); + it("falls back to a sentinel string instead of throwing if stringify explodes", () => { const trap: Record = {}; Object.defineProperty(trap, "boom", { @@ -128,6 +144,10 @@ describe("safeFormatTimestamp", () => { expect(safeFormatTimestamp(null)).toBe("—"); }); + it("falls back for dashed non-date strings", () => { + expect(safeFormatTimestamp("not-a-date", "fallback")).toBe("fallback"); + }); + it("honours a custom fallback string", () => { expect(safeFormatTimestamp(NaN, "n/a")).toBe("n/a"); });