Files
openclaw/src/utils/timer-delay.test.ts
2026-05-09 03:59:47 +01:00

36 lines
1.1 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
import {
MAX_SAFE_TIMEOUT_DELAY_MS,
resolveSafeTimeoutDelayMs,
setSafeTimeout,
} from "./timer-delay.js";
describe("resolveSafeTimeoutDelayMs", () => {
it("clamps to Node's signed-32-bit timer ceiling", () => {
expect(resolveSafeTimeoutDelayMs(3_000_000_000)).toBe(MAX_SAFE_TIMEOUT_DELAY_MS);
});
it("respects custom minimums", () => {
expect(resolveSafeTimeoutDelayMs(10, { minMs: 250 })).toBe(250);
expect(resolveSafeTimeoutDelayMs(10, { minMs: 0 })).toBe(10);
});
it("falls back to the minimum for non-finite input", () => {
expect(resolveSafeTimeoutDelayMs(Number.POSITIVE_INFINITY, { minMs: 250 })).toBe(250);
expect(resolveSafeTimeoutDelayMs(Number.NaN)).toBe(1);
});
});
describe("setSafeTimeout", () => {
it("arms setTimeout with the clamped delay", () => {
const timeoutSpy = vi.spyOn(globalThis, "setTimeout");
const callback = () => undefined;
const timer = setSafeTimeout(callback, 3_000_000_000);
clearTimeout(timer);
expect(timeoutSpy).toHaveBeenCalledWith(callback, MAX_SAFE_TIMEOUT_DELAY_MS);
timeoutSpy.mockRestore();
});
});