import { vi } from 'vitest'; import type { FastifyInstance } from 'fastify'; import type { NodePgDatabase } from 'drizzle-orm/node-postgres'; import type * as schema from '../src/db/schema/index.js'; export type MockDb = { select: ReturnType['select']>; insert: ReturnType['insert']>; update: ReturnType['update']>; delete: ReturnType['delete']>; }; /** * Create a chainable mock for Drizzle DB operations. * Configure chain terminals (limit, returning) to resolve to desired values. * Example: mockDb.select.mockReturnValue({ from: vi.fn().mockReturnValue({ where: vi.fn().mockReturnValue({ limit: vi.fn().mockResolvedValue([user]) }) }) }) */ export function createMockDb(): MockDb { const chain = { from: vi.fn().mockReturnThis(), where: vi.fn().mockReturnThis(), values: vi.fn().mockReturnThis(), set: vi.fn().mockReturnThis(), orderBy: vi.fn().mockReturnThis(), limit: vi.fn().mockReturnThis(), returning: vi.fn().mockReturnThis(), }; return { select: vi.fn().mockReturnValue(chain), insert: vi.fn().mockReturnValue(chain), update: vi.fn().mockReturnValue(chain), delete: vi.fn().mockReturnValue(chain), } as unknown as MockDb; } /** * Create a minimal mock Fastify app with db for route/integration tests. * Use buildApp() from app.ts for full integration tests. */ export function createMockApp(): FastifyInstance & { db: MockDb } { return { db: createMockDb(), log: { child: () => ({ debug: () => {}, info: () => {}, warn: () => {}, error: () => {}, trace: () => {}, fatal: () => {}, }), debug: () => {}, info: () => {}, warn: () => {}, error: () => {}, trace: () => {}, fatal: () => {}, }, } as FastifyInstance & { db: MockDb }; }