|
| 1 | +import type { DatabaseSync } from "node:sqlite"; |
| 2 | +import type { |
| 3 | +OpenClawConfig, |
| 4 | +ResolvedMemorySearchConfig, |
| 5 | +} from "openclaw/plugin-sdk/memory-core-host-engine-foundation"; |
| 6 | +import type { MemorySource } from "openclaw/plugin-sdk/memory-core-host-engine-storage"; |
| 7 | +import { MAX_TIMER_TIMEOUT_MS } from "openclaw/plugin-sdk/number-runtime"; |
| 8 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 9 | +import { MemoryManagerSyncOps } from "./manager-sync-ops.js"; |
| 10 | + |
| 11 | +type MemoryIndexEntry = { |
| 12 | +path: string; |
| 13 | +absPath: string; |
| 14 | +mtimeMs: number; |
| 15 | +size: number; |
| 16 | +hash: string; |
| 17 | +content?: string; |
| 18 | +}; |
| 19 | + |
| 20 | +class IntervalSyncHarness extends MemoryManagerSyncOps { |
| 21 | +protected readonly cfg = {} as OpenClawConfig; |
| 22 | +protected readonly agentId = "main"; |
| 23 | +protected readonly workspaceDir = "/tmp/openclaw-memory-interval-test"; |
| 24 | +protected readonly settings: ResolvedMemorySearchConfig; |
| 25 | +protected readonly batch = { |
| 26 | +enabled: false, |
| 27 | +wait: false, |
| 28 | +concurrency: 1, |
| 29 | +pollIntervalMs: 0, |
| 30 | +timeoutMs: 0, |
| 31 | +}; |
| 32 | +protected readonly vector = { enabled: false, available: false }; |
| 33 | +protected readonly cache = { enabled: false }; |
| 34 | +protected providerUnavailableReason?: string; |
| 35 | +protected providerLifecycle = { mode: "active" as const, providerId: "test" }; |
| 36 | +protected db = {} as DatabaseSync; |
| 37 | + |
| 38 | +constructor(intervalMinutes: number) { |
| 39 | +super(); |
| 40 | +this.settings = { |
| 41 | +sync: { intervalMinutes }, |
| 42 | +} as ResolvedMemorySearchConfig; |
| 43 | +} |
| 44 | + |
| 45 | +arm(): void { |
| 46 | +this.ensureIntervalSync(); |
| 47 | +} |
| 48 | + |
| 49 | +stop(): void { |
| 50 | +if (this.intervalTimer) { |
| 51 | +clearInterval(this.intervalTimer); |
| 52 | +this.intervalTimer = null; |
| 53 | +} |
| 54 | +} |
| 55 | + |
| 56 | +protected computeProviderKey(): string { |
| 57 | +return "test"; |
| 58 | +} |
| 59 | + |
| 60 | +protected async sync(): Promise<void> {} |
| 61 | + |
| 62 | +protected async withTimeout<T>(promise: Promise<T>): Promise<T> { |
| 63 | +return await promise; |
| 64 | +} |
| 65 | + |
| 66 | +protected getIndexConcurrency(): number { |
| 67 | +return 1; |
| 68 | +} |
| 69 | + |
| 70 | +protected pruneEmbeddingCacheIfNeeded(): void {} |
| 71 | + |
| 72 | +protected resetProviderInitializationForRetry(): void {} |
| 73 | + |
| 74 | +protected async indexFile( |
| 75 | +_entry: MemoryIndexEntry, |
| 76 | +_options: { source: MemorySource; content?: string }, |
| 77 | +): Promise<void> {} |
| 78 | +} |
| 79 | + |
| 80 | +describe("MemoryManagerSyncOps interval sync", () => { |
| 81 | +afterEach(() => { |
| 82 | +vi.useRealTimers(); |
| 83 | +}); |
| 84 | + |
| 85 | +it("clamps oversized interval sync timers", () => { |
| 86 | +vi.useFakeTimers(); |
| 87 | +const setIntervalSpy = vi.spyOn(globalThis, "setInterval"); |
| 88 | +const harness = new IntervalSyncHarness(Number.MAX_SAFE_INTEGER); |
| 89 | + |
| 90 | +harness.arm(); |
| 91 | + |
| 92 | +expect(setIntervalSpy).toHaveBeenCalledWith(expect.any(Function), MAX_TIMER_TIMEOUT_MS); |
| 93 | +harness.stop(); |
| 94 | +}); |
| 95 | +}); |