Skip to content

Commit defbf02

Browse files
committed
test(store): fix batching test for addDocuments to mock library id resolution
Ensure the batching test for addDocuments correctly mocks the library id lookup and re-instantiates DocumentStore after patching the mock, preventing StoreError for test-lib-large-batch. This makes the test robust and prevents false negatives when batching embeddings.
1 parent 3f93ecb commit defbf02

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

src/store/DocumentStore.test.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -285,13 +285,31 @@ describe("DocumentStore", () => {
285285
.mockResolvedValueOnce(firstBatchEmbeddings)
286286
.mockResolvedValueOnce(secondBatchEmbeddings);
287287

288-
// Mock insertDocument to return sequential rowids
289-
for (let i = 0; i < numDocuments; i++) {
290-
mockStatement.run.mockReturnValueOnce({
291-
changes: 1,
292-
lastInsertRowid: BigInt(i + 1),
293-
});
294-
}
288+
// Patch mockPrepare for this test to handle library id resolution for test-lib-large-batch
289+
const originalMockPrepare = mockPrepare;
290+
mockPrepare = vi.fn((sql: string) => {
291+
if (sql.includes("SELECT id FROM libraries WHERE name = ?")) {
292+
return {
293+
get: (name: string) =>
294+
name === "test-lib-large-batch" ? { id: 1 } : undefined,
295+
run: vi.fn(),
296+
all: mockStatementAll,
297+
};
298+
}
299+
if (sql.includes("INSERT INTO libraries")) {
300+
return {
301+
run: vi.fn(),
302+
get: vi.fn(),
303+
all: mockStatementAll,
304+
};
305+
}
306+
return originalMockPrepare(sql);
307+
});
308+
mockDb.prepare = (...args: unknown[]) => mockPrepare(...args);
309+
310+
// Re-instantiate DocumentStore after patching mockPrepare
311+
documentStore = new DocumentStore(":memory:");
312+
await documentStore.initialize();
295313

296314
await documentStore.addDocuments("test-lib-large-batch", "1.0.0", documents);
297315

0 commit comments

Comments
 (0)