From 3442acbae1c2d3d3ee948882d3e952bf3262bab6 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Fri, 13 Mar 2026 19:30:46 +0000 Subject: [PATCH] test: add normalization and backoff helper coverage --- src/infra/backoff.test.ts | 36 ++++++++++++++++++++++++++ src/infra/system-run-normalize.test.ts | 17 ++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 src/infra/backoff.test.ts create mode 100644 src/infra/system-run-normalize.test.ts diff --git a/src/infra/backoff.test.ts b/src/infra/backoff.test.ts new file mode 100644 index 000000000..9181d8324 --- /dev/null +++ b/src/infra/backoff.test.ts @@ -0,0 +1,36 @@ +import { describe, expect, it, vi } from "vitest"; +import { computeBackoff, sleepWithAbort, type BackoffPolicy } from "./backoff.js"; + +describe("backoff helpers", () => { + const policy: BackoffPolicy = { + initialMs: 100, + maxMs: 250, + factor: 2, + jitter: 0.5, + }; + + it("treats attempts below one as the first backoff step", () => { + const randomSpy = vi.spyOn(Math, "random").mockReturnValue(0); + try { + expect(computeBackoff(policy, 0)).toBe(100); + expect(computeBackoff(policy, 1)).toBe(100); + } finally { + randomSpy.mockRestore(); + } + }); + + it("adds jitter and clamps to maxMs", () => { + const randomSpy = vi.spyOn(Math, "random").mockReturnValue(1); + try { + expect(computeBackoff(policy, 2)).toBe(250); + expect(computeBackoff({ ...policy, maxMs: 450 }, 2)).toBe(300); + } finally { + randomSpy.mockRestore(); + } + }); + + it("returns immediately for non-positive sleep durations", async () => { + await expect(sleepWithAbort(0, AbortSignal.abort())).resolves.toBeUndefined(); + await expect(sleepWithAbort(-5)).resolves.toBeUndefined(); + }); +}); diff --git a/src/infra/system-run-normalize.test.ts b/src/infra/system-run-normalize.test.ts new file mode 100644 index 000000000..6bf2f56d4 --- /dev/null +++ b/src/infra/system-run-normalize.test.ts @@ -0,0 +1,17 @@ +import { describe, expect, it } from "vitest"; +import { normalizeNonEmptyString, normalizeStringArray } from "./system-run-normalize.js"; + +describe("system run normalization helpers", () => { + it("normalizes only non-empty trimmed strings", () => { + expect(normalizeNonEmptyString(" hello ")).toBe("hello"); + expect(normalizeNonEmptyString(" \n\t ")).toBeNull(); + expect(normalizeNonEmptyString(42)).toBeNull(); + expect(normalizeNonEmptyString(null)).toBeNull(); + }); + + it("normalizes array entries and rejects non-arrays", () => { + expect(normalizeStringArray([" alpha ", 42, false])).toEqual([" alpha ", "42", "false"]); + expect(normalizeStringArray(undefined)).toEqual([]); + expect(normalizeStringArray("alpha")).toEqual([]); + }); +});