diff --git a/src/infra/backoff.test.ts b/src/infra/backoff.test.ts new file mode 100644 index 00000000000..9181d832402 --- /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 00000000000..6bf2f56d4e9 --- /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([]); + }); +});