test: add normalization and backoff helper coverage

This commit is contained in:
Peter Steinberger
2026-03-13 19:30:46 +00:00
parent cab2f891e7
commit 3442acbae1
2 changed files with 53 additions and 0 deletions

36
src/infra/backoff.test.ts Normal file
View File

@@ -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();
});
});

View File

@@ -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([]);
});
});