test(memory): share short-timeout test helper

This commit is contained in:
Peter Steinberger
2026-02-21 22:35:22 +00:00
parent b257ba9e30
commit b6ce5e06cd
3 changed files with 22 additions and 30 deletions

View File

@@ -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]>;

View File

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

View File

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