Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/lib/__tests__/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]");
Expand All @@ -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<string, unknown> = {};
Object.defineProperty(trap, "boom", {
Expand Down Expand Up @@ -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");
});
Expand Down