From b6ce5e06cdb0310b363a42c5127140153ae01ea7 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 21 Feb 2026 22:35:22 +0000 Subject: [PATCH] test(memory): share short-timeout test helper --- src/memory/manager.batch.test.ts | 17 +---------------- src/memory/manager.embedding-batches.test.ts | 18 ++++-------------- test/helpers/fast-short-timeouts.ts | 17 +++++++++++++++++ 3 files changed, 22 insertions(+), 30 deletions(-) create mode 100644 test/helpers/fast-short-timeouts.ts diff --git a/src/memory/manager.batch.test.ts b/src/memory/manager.batch.test.ts index 2ed5ad0b733..dd08b03107e 100644 --- a/src/memory/manager.batch.test.ts +++ b/src/memory/manager.batch.test.ts @@ -2,6 +2,7 @@ import fs from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; +import { useFastShortTimeouts } from "../../test/helpers/fast-short-timeouts.js"; import type { OpenClawConfig } from "../config/config.js"; import { getMemorySearchManager, type MemoryIndexManager } from "./index.js"; import { createOpenAIEmbeddingProviderMock } from "./test-embeddings-mock.js"; @@ -25,22 +26,6 @@ describe("memory indexing with OpenAI batches", () => { let indexPath: string; let manager: MemoryIndexManager | null = null; - function useFastShortTimeouts() { - const realSetTimeout = setTimeout; - const spy = vi.spyOn(global, "setTimeout").mockImplementation((( - handler: TimerHandler, - timeout?: number, - ...args: unknown[] - ) => { - const delay = typeof timeout === "number" ? timeout : 0; - if (delay > 0 && delay <= 2000) { - return realSetTimeout(handler, 0, ...args); - } - return realSetTimeout(handler, delay, ...args); - }) as typeof setTimeout); - return () => spy.mockRestore(); - } - async function readOpenAIBatchUploadRequests(body: FormData) { let uploadedRequests: Array<{ custom_id?: string }> = []; const entries = body.entries() as IterableIterator<[string, FormDataEntryValue]>; diff --git a/src/memory/manager.embedding-batches.test.ts b/src/memory/manager.embedding-batches.test.ts index 445d4329233..602f9120714 100644 --- a/src/memory/manager.embedding-batches.test.ts +++ b/src/memory/manager.embedding-batches.test.ts @@ -1,6 +1,7 @@ import fs from "node:fs/promises"; import path from "node:path"; -import { describe, expect, it, vi } from "vitest"; +import { describe, expect, it } from "vitest"; +import { useFastShortTimeouts } from "../../test/helpers/fast-short-timeouts.js"; import { installEmbeddingManagerFixture } from "./embedding-manager.test-harness.js"; const fx = installEmbeddingManagerFixture({ @@ -88,22 +89,11 @@ describe("memory embedding batches", () => { return texts.map(() => [0, 1, 0]); }); - const realSetTimeout = setTimeout; - const setTimeoutSpy = vi.spyOn(global, "setTimeout").mockImplementation((( - handler: TimerHandler, - timeout?: number, - ...args: unknown[] - ) => { - const delay = typeof timeout === "number" ? timeout : 0; - if (delay > 0 && delay <= 2000) { - return realSetTimeout(handler, 0, ...args); - } - return realSetTimeout(handler, delay, ...args); - }) as typeof setTimeout); + const restoreFastTimeouts = useFastShortTimeouts(); try { await managerSmall.sync({ reason: "test" }); } finally { - setTimeoutSpy.mockRestore(); + restoreFastTimeouts(); } expect(calls).toBe(3); diff --git a/test/helpers/fast-short-timeouts.ts b/test/helpers/fast-short-timeouts.ts new file mode 100644 index 00000000000..66ff38061fa --- /dev/null +++ b/test/helpers/fast-short-timeouts.ts @@ -0,0 +1,17 @@ +import { vi } from "vitest"; + +export function useFastShortTimeouts(maxDelayMs = 2000): () => void { + const realSetTimeout = setTimeout; + const spy = vi.spyOn(global, "setTimeout").mockImplementation((( + handler: TimerHandler, + timeout?: number, + ...args: unknown[] + ) => { + const delay = typeof timeout === "number" ? timeout : 0; + if (delay > 0 && delay <= maxDelayMs) { + return realSetTimeout(handler, 0, ...args); + } + return realSetTimeout(handler, delay, ...args); + }) as typeof setTimeout); + return () => spy.mockRestore(); +}