// tests/helpers/createTestDb.ts // in-memory SQLite + drizzle-orm 스키마 적용 import Database from 'better-sqlite3' import { drizzle, type BetterSQLite3Database } from 'drizzle-orm/better-sqlite3' import * as schema from '../../src/main/db/schema' /** * 테스트용 in-memory SQLite DB를 생성한다. * 각 테스트에서 독립적인 DB를 사용할 수 있다. */ export function createTestDb(): { db: BetterSQLite3Database sqlite: Database.Database close: () => void } { const sqlite = new Database(':memory:') sqlite.pragma('journal_mode = WAL') sqlite.pragma('foreign_keys = ON') // 테이블 생성 (src/main/db/index.ts의 SQL과 동일) sqlite.exec(` CREATE TABLE IF NOT EXISTS history ( id TEXT PRIMARY KEY, original_text TEXT NOT NULL, polished_text TEXT, focused_app TEXT, focused_app_name TEXT, focused_app_window_title TEXT, mode TEXT NOT NULL DEFAULT 'dictation', status TEXT NOT NULL DEFAULT 'completed', error_code TEXT, audio_local_path TEXT, duration REAL NOT NULL, detected_language TEXT, mic_device TEXT, word_count INTEGER NOT NULL DEFAULT 0, stt_model TEXT, llm_model TEXT, stt_latency_ms INTEGER, llm_latency_ms INTEGER, created_at INTEGER NOT NULL, updated_at INTEGER NOT NULL, app_version TEXT NOT NULL DEFAULT '1.0.0' ); CREATE INDEX IF NOT EXISTS idx_history_created_at ON history(created_at DESC); CREATE INDEX IF NOT EXISTS idx_history_status ON history(status); CREATE TABLE IF NOT EXISTS dictionary ( id TEXT PRIMARY KEY, word TEXT NOT NULL, pronunciation TEXT, category TEXT NOT NULL DEFAULT 'user', usage_count INTEGER NOT NULL DEFAULT 0, last_used_at INTEGER, created_at INTEGER NOT NULL, updated_at INTEGER NOT NULL ); CREATE UNIQUE INDEX IF NOT EXISTS idx_dictionary_word_category ON dictionary(word, category); CREATE INDEX IF NOT EXISTS idx_dictionary_created_at ON dictionary(created_at); CREATE INDEX IF NOT EXISTS idx_dictionary_usage_count ON dictionary(usage_count DESC); CREATE TABLE IF NOT EXISTS stats ( id INTEGER PRIMARY KEY CHECK (id = 1), total_duration REAL NOT NULL DEFAULT 0, total_words INTEGER NOT NULL DEFAULT 0, session_count INTEGER NOT NULL DEFAULT 0, streak_days INTEGER NOT NULL DEFAULT 0, last_session_at INTEGER, last_updated INTEGER NOT NULL ); INSERT OR IGNORE INTO stats (id, total_duration, total_words, session_count, streak_days, last_updated) VALUES (1, 0, 0, 0, 0, ${Date.now()}); `) const db = drizzle(sqlite, { schema }) return { db, sqlite, close: () => sqlite.close() } }