import { describe, it, expect, beforeEach, afterEach } from 'vitest' import { mkdtemp, rm, readFile } from 'node:fs/promises' import { tmpdir } from 'node:os' import path from 'node:path' import { initializeDatabase } from '../../src/db/schema' import { getRepoById } from '../../src/db/queries' describe('initializeDatabase', () => { let tempDir: string let dbPath: string beforeEach(async () => { tempDir = await mkdtemp(path.join(tmpdir(), 'ocm-db-')) dbPath = path.join(tempDir, 'nested', 'opencode.db') }) afterEach(async () => { await rm(tempDir, { recursive: true, force: true }) }) it('creates the database file, applies migrations, and seeds defaults', async () => { const db = initializeDatabase(dbPath) const migrations = db.prepare('SELECT id AS name FROM applied_migrations').all() as Array<{ name: string }> expect(migrations.map(migration => migration.name)).toContain('001-base-schema') expect(migrations.map(migration => migration.name)).toContain('019-drop-opencode-configs') const preferences = db.prepare('SELECT preferences FROM user_preferences WHERE user_id = ?').get('default') as { preferences: string } expect(preferences.preferences).toBe('{}') expect(getRepoById(db, 0)?.localPath).toBe('assistant') db.close() const fileContents = await readFile(dbPath, 'utf-8') expect(fileContents.length).toBeGreaterThan(0) }) })