|
| 1 | +import * as fs from 'node:fs'; |
| 2 | +import os from 'node:os'; |
| 3 | +import * as path from 'node:path'; |
| 4 | +import Database from 'better-sqlite3'; |
| 5 | +import { drizzle } from 'drizzle-orm/better-sqlite3'; |
| 6 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 7 | +import { createDb, ensureDirectoryExists, getDbPath, runMigrations } from '../db.js'; |
| 8 | + |
| 9 | +// Mock the migrator module |
| 10 | +vi.mock('drizzle-orm/better-sqlite3/migrator', () => ({ |
| 11 | + migrate: vi.fn(), |
| 12 | +})); |
| 13 | + |
| 14 | +// Mock the logger using hoisted function |
| 15 | +const mockError = vi.hoisted(() => vi.fn()); |
| 16 | +vi.mock('@lytics/kero', () => ({ |
| 17 | + default: { |
| 18 | + createLogger: () => ({ |
| 19 | + error: mockError, |
| 20 | + warn: vi.fn(), |
| 21 | + info: vi.fn(), |
| 22 | + debug: vi.fn(), |
| 23 | + }), |
| 24 | + }, |
| 25 | +})); |
| 26 | + |
| 27 | +describe('db', () => { |
| 28 | + beforeEach(() => { |
| 29 | + mockError.mockClear(); |
| 30 | + }); |
| 31 | + |
| 32 | + describe('ensureDirectoryExists', () => { |
| 33 | + it('should create directory if it does not exist', () => { |
| 34 | + const tempDir = path.join(os.tmpdir(), `doc-agent-test-${Date.now()}`); |
| 35 | + expect(fs.existsSync(tempDir)).toBe(false); |
| 36 | + |
| 37 | + ensureDirectoryExists(tempDir); |
| 38 | + |
| 39 | + expect(fs.existsSync(tempDir)).toBe(true); |
| 40 | + |
| 41 | + // Cleanup |
| 42 | + fs.rmdirSync(tempDir); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should not fail if directory already exists', () => { |
| 46 | + const tempDir = path.join(os.tmpdir(), `doc-agent-test-${Date.now()}`); |
| 47 | + fs.mkdirSync(tempDir, { recursive: true }); |
| 48 | + |
| 49 | + expect(() => ensureDirectoryExists(tempDir)).not.toThrow(); |
| 50 | + expect(fs.existsSync(tempDir)).toBe(true); |
| 51 | + |
| 52 | + // Cleanup |
| 53 | + fs.rmdirSync(tempDir); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + describe('getDbPath', () => { |
| 58 | + it('should return a valid database path', () => { |
| 59 | + const dbPath = getDbPath(); |
| 60 | + expect(dbPath).toBeTruthy(); |
| 61 | + expect(dbPath).toContain('doc-agent.db'); |
| 62 | + expect(typeof dbPath).toBe('string'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should accept custom data directory', () => { |
| 66 | + const customDir = path.join(os.tmpdir(), `doc-agent-custom-${Date.now()}`); |
| 67 | + const dbPath = getDbPath(customDir); |
| 68 | + |
| 69 | + expect(dbPath).toContain('doc-agent.db'); |
| 70 | + expect(dbPath).toContain(customDir); |
| 71 | + expect(fs.existsSync(customDir)).toBe(true); |
| 72 | + |
| 73 | + // Cleanup |
| 74 | + fs.rmdirSync(customDir); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe('runMigrations', () => { |
| 79 | + it('should handle migration failures gracefully', async () => { |
| 80 | + const { migrate } = await import('drizzle-orm/better-sqlite3/migrator'); |
| 81 | + const db = drizzle(new Database(':memory:'), { schema: {} }); |
| 82 | + |
| 83 | + // Create a temp directory that exists but has no valid migrations |
| 84 | + const tempMigrationsDir = path.join(os.tmpdir(), `migrations-test-${Date.now()}`); |
| 85 | + fs.mkdirSync(tempMigrationsDir, { recursive: true }); |
| 86 | + |
| 87 | + // Make migrate throw an error |
| 88 | + vi.mocked(migrate).mockImplementation(() => { |
| 89 | + throw new Error('Test migration failure'); |
| 90 | + }); |
| 91 | + |
| 92 | + // This should not throw, just log the error |
| 93 | + expect(() => runMigrations(db, tempMigrationsDir)).not.toThrow(); |
| 94 | + expect(mockError).toHaveBeenCalledWith(expect.any(Error), 'Migration failed'); |
| 95 | + |
| 96 | + // Cleanup |
| 97 | + fs.rmdirSync(tempMigrationsDir); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should skip migrations if folder does not exist', () => { |
| 101 | + const db = drizzle(new Database(':memory:'), { schema: {} }); |
| 102 | + const nonExistentDir = path.join(os.tmpdir(), `non-existent-${Date.now()}`); |
| 103 | + |
| 104 | + // Should not throw |
| 105 | + expect(() => runMigrations(db, nonExistentDir)).not.toThrow(); |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + describe('createDb', () => { |
| 110 | + it('should accept custom connection string', () => { |
| 111 | + const db = createDb(':memory:'); |
| 112 | + expect(db).toBeDefined(); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should use default path when no connection string provided', () => { |
| 116 | + const db = createDb(); |
| 117 | + expect(db).toBeDefined(); |
| 118 | + }); |
| 119 | + }); |
| 120 | +}); |
0 commit comments