|
6 | 6 | * namespace scoping, tag search, TTL expiration, guarded writes. |
7 | 7 | */ |
8 | 8 |
|
9 | | -import { describe, it, expect, beforeEach, afterEach } from "vitest"; |
| 9 | +import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; |
10 | 10 | import { HnswLite, cosineSimilarity } from "./hnsw.js"; |
11 | 11 | import { KnowledgeGraph } from "./graph.js"; |
12 | 12 | import type { Entity, Relation } from "./graph.js"; |
@@ -259,6 +259,7 @@ describe("shared", () => { |
259 | 259 |
|
260 | 260 | afterEach(() => { |
261 | 261 | memory.dispose(); |
| 262 | + vi.restoreAllMocks(); |
262 | 263 | }); |
263 | 264 |
|
264 | 265 | it("store + get roundtrip", async () => { |
@@ -323,6 +324,40 @@ describe("shared", () => { |
323 | 324 | expect(resultsA).toHaveLength(1); |
324 | 325 | expect(resultsB).toHaveLength(1); |
325 | 326 | }); |
| 327 | + |
| 328 | + it("opens IndexedDB and persists entries when enableIdb is true", async () => { |
| 329 | + const close = vi.fn(); |
| 330 | + const openSpy = vi |
| 331 | + .spyOn(IdbBackend.prototype, "open") |
| 332 | + .mockImplementation(async function mockOpen(this: IdbBackend) { |
| 333 | + (this as any).db = { close } as IDBDatabase; |
| 334 | + return true; |
| 335 | + }); |
| 336 | + const putSpy = vi.spyOn(IdbBackend.prototype, "put").mockResolvedValue(); |
| 337 | + |
| 338 | + const idbMemory = new SharedMemory(events, { |
| 339 | + dimensions: 3, |
| 340 | + enableIdb: true, |
| 341 | + idbName: "test-memory", |
| 342 | + guardEvaluator: makeAllowEvaluator(), |
| 343 | + }); |
| 344 | + |
| 345 | + try { |
| 346 | + const stored = await idbMemory.store("ns", "persisted", { data: "hello" }); |
| 347 | + expect(stored).toBe(true); |
| 348 | + expect(openSpy).toHaveBeenCalledOnce(); |
| 349 | + expect(putSpy).toHaveBeenCalledWith( |
| 350 | + "ns:persisted", |
| 351 | + expect.objectContaining({ |
| 352 | + namespace: "ns", |
| 353 | + value: { data: "hello" }, |
| 354 | + _key: "ns:persisted", |
| 355 | + }), |
| 356 | + ); |
| 357 | + } finally { |
| 358 | + idbMemory.dispose(); |
| 359 | + } |
| 360 | + }); |
326 | 361 | }); |
327 | 362 |
|
328 | 363 | // --------------------------------------------------------------------------- |
|
0 commit comments