Files
openclaw/src/memory/manager.sync-errors-do-not-crash.test.ts

76 lines
2.5 KiB
TypeScript
Raw Normal View History

2026-01-17 08:03:53 +00:00
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2026-02-17 15:46:48 +09:00
import type { OpenClawConfig } from "../config/config.js";
2026-02-18 01:29:02 +00:00
import { getEmbedBatchMock, resetEmbeddingMocks } from "./embedding.test-mocks.js";
import type { MemoryIndexManager } from "./index.js";
import { getRequiredMemoryIndexManager } from "./test-manager-helpers.js";
2026-01-17 08:03:53 +00:00
describe("memory manager sync failures", () => {
let workspaceDir: string;
let indexPath: string;
let manager: MemoryIndexManager | null = null;
const embedBatch = getEmbedBatchMock();
2026-01-17 08:03:53 +00:00
beforeEach(async () => {
vi.useFakeTimers();
resetEmbeddingMocks();
embedBatch.mockImplementation(async () => {
throw new Error("openai embeddings failed: 400 bad request");
});
2026-01-30 03:15:10 +01:00
workspaceDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-mem-"));
2026-01-17 08:03:53 +00:00
indexPath = path.join(workspaceDir, "index.sqlite");
await fs.mkdir(path.join(workspaceDir, "memory"));
await fs.writeFile(path.join(workspaceDir, "MEMORY.md"), "Hello");
});
afterEach(async () => {
vi.useRealTimers();
if (manager) {
await manager.close();
manager = null;
}
await fs.rm(workspaceDir, { recursive: true, force: true });
});
it("does not raise unhandledRejection when watch-triggered sync fails", async () => {
const unhandled: unknown[] = [];
const handler = (reason: unknown) => {
unhandled.push(reason);
};
process.on("unhandledRejection", handler);
const cfg = {
agents: {
defaults: {
workspace: workspaceDir,
memorySearch: {
provider: "openai",
model: "mock-embed",
store: { path: indexPath },
sync: { watch: true, watchDebounceMs: 1, onSessionStart: false, onSearch: false },
},
},
list: [{ id: "main", default: true }],
},
2026-02-17 15:46:48 +09:00
} as OpenClawConfig;
2026-01-17 08:03:53 +00:00
manager = await getRequiredMemoryIndexManager({ cfg, agentId: "main" });
const syncSpy = vi.spyOn(manager, "sync");
2026-01-17 08:03:53 +00:00
// Call the internal scheduler directly; it uses fire-and-forget sync.
(manager as unknown as { scheduleWatchSync: () => void }).scheduleWatchSync();
2026-01-17 23:01:03 +00:00
await vi.runOnlyPendingTimersAsync();
const syncPromise = syncSpy.mock.results[0]?.value as Promise<void> | undefined;
2026-01-17 23:01:03 +00:00
vi.useRealTimers();
if (syncPromise) {
await syncPromise.catch(() => undefined);
}
2026-01-17 08:03:53 +00:00
process.off("unhandledRejection", handler);
expect(unhandled).toHaveLength(0);
});
});