mirror of
https://github.com/openclaw/openclaw.git
synced 2026-06-08 12:42:53 +00:00
53 lines
1.7 KiB
TypeScript
53 lines
1.7 KiB
TypeScript
import { MAX_TIMER_TIMEOUT_MS } from "openclaw/plugin-sdk/number-runtime";
|
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
import { raceWithTimeoutAndAbort, waitForAbortableDelay } from "./async.js";
|
|
|
|
afterEach(() => {
|
|
vi.useRealTimers();
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
describe("raceWithTimeoutAndAbort", () => {
|
|
it("normalizes oversized timeouts before arming the watchdog", async () => {
|
|
const timeoutSpy = vi
|
|
.spyOn(globalThis, "setTimeout")
|
|
.mockReturnValue(1 as unknown as ReturnType<typeof setTimeout>);
|
|
vi.spyOn(globalThis, "clearTimeout").mockImplementation(() => undefined);
|
|
|
|
await raceWithTimeoutAndAbort(Promise.resolve("ok"), {
|
|
timeoutMs: Number.MAX_SAFE_INTEGER,
|
|
});
|
|
|
|
expect(timeoutSpy).toHaveBeenCalledWith(expect.any(Function), MAX_TIMER_TIMEOUT_MS);
|
|
});
|
|
});
|
|
|
|
describe("waitForAbortableDelay", () => {
|
|
it("resolves false immediately when already aborted", async () => {
|
|
vi.useFakeTimers();
|
|
const abortController = new AbortController();
|
|
abortController.abort();
|
|
|
|
await expect(waitForAbortableDelay(60_000, abortController.signal)).resolves.toBe(false);
|
|
});
|
|
|
|
it("resolves false immediately when aborted during backoff", async () => {
|
|
vi.useFakeTimers();
|
|
const abortController = new AbortController();
|
|
|
|
const delay = waitForAbortableDelay(60_000, abortController.signal);
|
|
abortController.abort();
|
|
|
|
await expect(delay).resolves.toBe(false);
|
|
});
|
|
|
|
it("resolves true after the full delay when not aborted", async () => {
|
|
vi.useFakeTimers();
|
|
|
|
const delay = waitForAbortableDelay(500);
|
|
await vi.advanceTimersByTimeAsync(500);
|
|
|
|
await expect(delay).resolves.toBe(true);
|
|
});
|
|
});
|