|
1 | 1 | import { vi } from 'vitest' |
2 | 2 | import * as sqliteStorage from './sqlite-storage'; |
3 | 3 |
|
4 | | -// Mock sqlite3 to prevent actual database operations |
| 4 | +// Mock better-sqlite3 to prevent actual database operations |
5 | 5 | // In vitest v4, mocks used as constructors must use 'function' or 'class' syntax |
6 | | -vi.mock('sqlite3', () => ({ |
7 | | - default: { |
8 | | - Database: vi.fn(function() { |
| 6 | +vi.mock('better-sqlite3', () => { |
| 7 | + const mockStatement = { |
| 8 | + run: vi.fn(() => ({ lastInsertRowid: 1, changes: 1 })), |
| 9 | + get: vi.fn(() => undefined), |
| 10 | + all: vi.fn(() => []), |
| 11 | + } |
| 12 | + return { |
| 13 | + default: vi.fn(function() { |
9 | 14 | return { |
10 | | - run: vi.fn(function(sql: string, params: unknown, callback?: (this: { lastID: number; changes: number }) => void) { |
11 | | - if (callback) { |
12 | | - // Mock the 'this' context with lastID and changes |
13 | | - callback.call({ lastID: 1, changes: 1 }); |
14 | | - } |
15 | | - }), |
16 | | - get: vi.fn((sql: string, params: unknown, callback?: (err: Error | null, row: unknown) => void) => { |
17 | | - if (callback) callback(null, null); |
18 | | - }), |
19 | | - all: vi.fn((sql: string, params: unknown, callback?: (err: Error | null, rows: unknown[]) => void) => { |
20 | | - if (callback) callback(null, []); |
21 | | - }), |
22 | | - close: vi.fn((callback?: () => void) => { |
23 | | - if (callback) callback(); |
24 | | - }), |
25 | | - serialize: vi.fn((fn?: () => void) => { |
26 | | - if (fn) fn(); |
27 | | - }) |
28 | | - }; |
| 15 | + prepare: vi.fn(() => mockStatement), |
| 16 | + exec: vi.fn(), |
| 17 | + close: vi.fn(), |
| 18 | + } |
29 | 19 | }) |
30 | 20 | } |
31 | | -})); |
| 21 | +}); |
32 | 22 |
|
33 | 23 | // Integration-style tests for SQLite storage |
34 | 24 | // These tests verify the public API without heavy mocking |
|
0 commit comments