MCPcopy
hub / github.com/CodebuffAI/codebuff / setupDbSpies

Function setupDbSpies

common/src/testing/mocks/database.ts:269–303  ·  view source on GitHub ↗
(
  db: { insert: unknown; update: unknown },
  options: CreateMockDbOptions = {},
)

Source from the content-addressed store, hash-verified

267 * Accepts any object with insert and update methods.
268 */
269export function setupDbSpies(
270 db: { insert: unknown; update: unknown },
271 options: CreateMockDbOptions = {},
272): DbSpies {
273 const { defaultInsertId = 'test-run-id' } = options
274
275 const mockInsertResult = {
276 values: mock(() => Promise.resolve({ id: defaultInsertId })),
277 }
278
279 const mockUpdateResult = {
280 set: mock(() => ({
281 where: mock(() => Promise.resolve()),
282 })),
283 }
284
285 // Cast db to a spyable type - the actual db module has complex types that
286 // don't play well with spyOn's inference, but the spy still works at runtime
287 const spyableDb = db as { insert: () => unknown; update: () => unknown }
288 const insertSpy = spyOn(spyableDb, 'insert').mockReturnValue(mockInsertResult)
289 const updateSpy = spyOn(spyableDb, 'update').mockReturnValue(mockUpdateResult)
290
291 return {
292 insert: insertSpy,
293 update: updateSpy,
294 restore: () => {
295 insertSpy.mockRestore()
296 updateSpy.mockRestore()
297 },
298 clear: () => {
299 insertSpy.mockClear()
300 updateSpy.mockClear()
301 },
302 }
303}
304
305/**
306 * Creates a mock for a database query builder chain that returns specific data.

Callers 3

beforeEachFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected