/** * Mock D1 Database for testing Cloudflare Workers * * Provides a mock implementation of D1Database that tracks queries * and allows custom mock responses for testing. * * @example * ```typescript * const db = createMockD1Database(); * * // Setup mock responses using regex patterns (TEST-DESIGN-002) * // Use /^\s*SELECT/i to anchor to start and be case-insensitive * db._setupMock((query, bindings) => { * if (/^\s*SELECT/i.test(query) && query.includes('presets')) { * return [{ id: 'preset-1', name: 'Test Preset' }]; * } * if (/^\s*INSERT/i.test(query)) { * return { meta: { changes: 1, last_row_id: 1 } }; * } * return null; * }); * * // Use in tests * const env = { DB: db as unknown as D1Database }; * * // Assert queries were made * expect(db._queries).toContain('SELECT * FROM presets'); * expect(db._bindings[0]).toEqual(['param1']); * * // Reset between tests * db._reset(); * ``` */ /** * Function type for mocking query responses */ export type QueryMockFn = (query: string, bindings: unknown[]) => unknown; /** * Mock D1 prepared statement interface */ /** * D1 result type - matches Cloudflare's D1Result */ export interface D1Result { results: T[]; success: true; meta: D1Meta; } export interface MockD1PreparedStatement { bind: (...values: unknown[]) => MockD1PreparedStatement; first: () => Promise; all: () => Promise>; run: () => Promise>; raw: (options?: { columnNames?: boolean; }) => Promise; } /** * D1 result metadata - matches Cloudflare's D1Meta type * Includes index signature to match Record requirement */ interface D1Meta { duration: number; changes: number; last_row_id: number; rows_read: number; rows_written: number; size_after: number; changed_db: boolean; [key: string]: unknown; } /** * Mock D1 database session (returned by withSession) * Mimics D1DatabaseSession which has similar methods to D1Database */ export interface MockD1DatabaseSession { prepare: (query: string) => MockD1PreparedStatement; batch: (statements: MockD1PreparedStatement[]) => Promise[]>; exec: (query: string) => Promise<{ count: number; duration: number; }>; /** Get constraint string for this session */ getBookmark: () => string; } /** * Configuration options for the mock D1 database */ export interface MockD1DatabaseConfig { /** Maximum number of queries to keep in history (prevents memory leaks). Default: 1000 */ maxQueryHistory?: number; } /** * Extended mock D1 database with test helpers */ export interface MockD1Database { prepare: (query: string) => MockD1PreparedStatement; batch: (statements: MockD1PreparedStatement[]) => Promise[]>; exec: (query: string) => Promise<{ count: number; duration: number; }>; dump: () => Promise; /** Start a session - returns a session object with same query methods */ withSession: (constraintOrBookmark?: string) => MockD1DatabaseSession; /** Array of all queries executed (for assertions). Limited to maxQueryHistory entries. */ _queries: string[]; /** Array of all binding arrays passed to queries. Limited to maxQueryHistory entries. */ _bindings: unknown[][]; /** Setup a mock function to return custom responses */ _setupMock: (fn: QueryMockFn) => void; /** Reset queries, bindings, and mock function */ _reset: () => void; /** Current mock function (if any) */ _mockFn?: QueryMockFn; /** * Set the ban status for ban-check middleware tests * @param isBanned - true to simulate banned user, false or undefined for not banned */ _setBanStatus: (isBanned: boolean) => void; /** Set the maximum query history size (for memory management) */ _setMaxQueryHistory: (max: number) => void; } /** * Creates a mock D1 database for testing * * The mock tracks all queries and bindings for assertions, and supports * custom response functions for simulating database behavior. * * TEST-OPT-003 FIX: Added maxQueryHistory config to prevent unbounded memory growth * in long-running test suites. Default is 1000 queries. * * @param config - Optional configuration for the mock database * @returns A mock D1 database that can be cast to D1Database */ export declare function createMockD1Database(config?: MockD1DatabaseConfig): MockD1Database; /** * Creates a mock D1 database pre-cast as D1Database * * Use this when you need to pass the mock to code expecting D1Database. * The mock still supports all testing helpers via type assertion: * * @example * ```typescript * const db = createMockD1(); * // Use as D1Database * someFunction(db); * * // Access testing helpers via assertion * (db as unknown as MockD1Database)._queries; * (db as unknown as MockD1Database)._setupMock(...); * ``` * * @returns A D1Database-typed mock */ export declare function createMockD1(): D1Database; export {}; //# sourceMappingURL=d1.d.ts.map