mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-19 22:10:51 +00:00
test: add normalization and backoff helper coverage
This commit is contained in:
36
src/infra/backoff.test.ts
Normal file
36
src/infra/backoff.test.ts
Normal 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();
|
||||
});
|
||||
});
|
||||
17
src/infra/system-run-normalize.test.ts
Normal file
17
src/infra/system-run-normalize.test.ts
Normal 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([]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user