// src/main/db/index.ts // 사용자별 SQLite 격리 (SaaS 데스크톱 클라이언트 Phase 1) // // - 로그인 전: DB 미오픈. getDatabase() 호출 시 throw. // - 로그인 후: userData/d3ro-voice/users/${userId}/d3ro.db 로 분리. // - 로그아웃: DB close. // - CloudSyncService가 lifecycle 단일 진입점(_onAuthenticated / signOut)에서 호출. import Database from 'better-sqlite3' import { drizzle, type BetterSQLite3Database } from 'drizzle-orm/better-sqlite3' import { app } from 'electron' import fs from 'fs' import path from 'path' import * as schema from './schema' import { getLogger } from '../services/LoggerService' import { D3ROError, ErrorCode } from '@d3ro/core/errors' import { emitAccountScopeChanged, LOCAL_SCOPE_ID } from '../services/account-scope' const logger = getLogger('database') // ───────────────────────────────────────────────────────────── // 모듈 상태 // ───────────────────────────────────────────────────────────── /** * 로컬(익명) 모드 user id. OAuth 로그인 없이 동작하는 무료 로컬 모드의 * 데이터 디렉토리 식별자. `userData/users/_local/d3ro.db`에 저장된다. * * 비즈니스 모델 관점: * - `_local`: 무료 익명 사용자 entry point (킬러 피처 — 회원가입 0, 로컬 STT/LLM) * - 실제 uuid: OAuth 로그인 후 클라우드 sync + premium 기능 (유료 SaaS 수익화) */ export const LOCAL_USER_ID = LOCAL_SCOPE_ID let currentUserId: string | null = null let sqlite: Database.Database | null = null let db: BetterSQLite3Database | null = null // ───────────────────────────────────────────────────────────── // 경로 헬퍼 // ───────────────────────────────────────────────────────────── /** 사용자별 DB 파일 경로. */ function getUserDbPath(userId: string): string { // 안전 가드: userId에 path separator 들어가지 않도록 if (!userId || userId.includes('/') || userId.includes('\\') || userId.includes('..')) { throw new D3ROError(ErrorCode.DBOpenFailed, `Invalid userId for DB path: "${userId}"`) } return path.join(app.getPath('userData'), 'users', userId, 'd3ro.db') } /** 현재 모드가 익명 로컬 모드인지. */ export function isLocalMode(): boolean { return currentUserId === LOCAL_USER_ID } /** 레거시(V1) 단일 DB 파일 경로. 한 번도 정식 서비스되지 않았으므로 발견 시 silent archive. */ function getLegacyDbPath(): string { return path.join(app.getPath('userData'), 'd3ro-voice.db') } function archiveLegacyDbIfExists(): void { const legacyPath = getLegacyDbPath() if (!fs.existsSync(legacyPath)) return try { const ts = new Date() .toISOString() .replace(/[:.]/g, '-') .replace('T', '_') .replace('Z', '') const archivePath = path.join( app.getPath('userData'), `d3ro-voice.legacy-${ts}.db` ) fs.renameSync(legacyPath, archivePath) logger.info(`Legacy DB archived: ${legacyPath} -> ${archivePath}`) } catch (err) { logger.warn( `Legacy DB archive failed (leaving in place): ${err instanceof Error ? err.message : String(err)}` ) } } // ───────────────────────────────────────────────────────────── // 스키마 적용 (테이블 생성 + 인라인 ALTER 마이그레이션) // ───────────────────────────────────────────────────────────── export function applySchema(s: Database.Database): void { s.pragma('journal_mode = WAL') s.pragma('foreign_keys = ON') s.pragma('busy_timeout = 5000') s.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 INDEX IF NOT EXISTS idx_history_detected_language ON history(detected_language); CREATE INDEX IF NOT EXISTS idx_history_focused_app_name ON history(focused_app_name); CREATE INDEX IF NOT EXISTS idx_history_mode ON history(mode); 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()}); CREATE TABLE IF NOT EXISTS memo_tags ( id TEXT PRIMARY KEY, history_id TEXT NOT NULL, tag TEXT NOT NULL, created_at INTEGER NOT NULL ); CREATE UNIQUE INDEX IF NOT EXISTS idx_memo_tags_unique ON memo_tags(history_id, tag); CREATE INDEX IF NOT EXISTS idx_memo_tags_history_id ON memo_tags(history_id); CREATE INDEX IF NOT EXISTS idx_memo_tags_tag ON memo_tags(tag); CREATE TABLE IF NOT EXISTS daily_usage ( id INTEGER PRIMARY KEY AUTOINCREMENT, date TEXT NOT NULL, feature TEXT NOT NULL, count INTEGER NOT NULL DEFAULT 0 ); CREATE UNIQUE INDEX IF NOT EXISTS idx_daily_usage_date_feature ON daily_usage(date, feature); CREATE INDEX IF NOT EXISTS idx_daily_usage_date ON daily_usage(date); `) // Phase 13.2: RAG 테이블 s.exec(` CREATE TABLE IF NOT EXISTS rag_documents ( id TEXT PRIMARY KEY, file_name TEXT NOT NULL, file_path TEXT NOT NULL, file_type TEXT NOT NULL, chunk_count INTEGER NOT NULL DEFAULT 0, indexed INTEGER NOT NULL DEFAULT 0, indexed_at INTEGER, added_at INTEGER NOT NULL ); CREATE INDEX IF NOT EXISTS idx_rag_documents_added_at ON rag_documents(added_at); CREATE TABLE IF NOT EXISTS rag_chunks ( id TEXT PRIMARY KEY, document_id TEXT NOT NULL, content TEXT NOT NULL, embedding TEXT NOT NULL, chunk_index INTEGER NOT NULL ); CREATE INDEX IF NOT EXISTS idx_rag_chunks_document_id ON rag_chunks(document_id); `) // Phase 14: Meeting Mode 테이블 s.exec(` CREATE TABLE IF NOT EXISTS meeting_sessions ( id TEXT PRIMARY KEY, title TEXT, status TEXT NOT NULL DEFAULT 'recording', started_at INTEGER NOT NULL, ended_at INTEGER, duration_ms INTEGER, raw_transcript TEXT, minutes_markdown TEXT, minutes_json TEXT, stt_model TEXT, llm_model TEXT, stt_latency_ms INTEGER, llm_latency_ms INTEGER, error_message TEXT, created_at INTEGER NOT NULL, updated_at INTEGER NOT NULL ); CREATE INDEX IF NOT EXISTS idx_meeting_sessions_created_at ON meeting_sessions(created_at DESC); CREATE INDEX IF NOT EXISTS idx_meeting_sessions_status ON meeting_sessions(status); CREATE TABLE IF NOT EXISTS meeting_memos ( id TEXT PRIMARY KEY, session_id TEXT NOT NULL, content TEXT NOT NULL, timestamp_ms INTEGER NOT NULL, created_at INTEGER NOT NULL ); CREATE INDEX IF NOT EXISTS idx_meeting_memos_session_id ON meeting_memos(session_id); CREATE INDEX IF NOT EXISTS idx_meeting_memos_timestamp ON meeting_memos(timestamp_ms); `) // Phase 14.5: meeting_documents 테이블 s.exec(` CREATE TABLE IF NOT EXISTS meeting_documents ( id TEXT PRIMARY KEY, session_id TEXT NOT NULL, template_type TEXT NOT NULL, title TEXT NOT NULL, content TEXT NOT NULL DEFAULT '', prompt_used TEXT, llm_model TEXT, llm_latency_ms INTEGER, created_at INTEGER NOT NULL, updated_at INTEGER NOT NULL ); CREATE INDEX IF NOT EXISTS idx_meeting_documents_session_id ON meeting_documents(session_id); `) // 입력 인텔리전스: 텔레메트리 집계 / 학습 문장 / 개인 문구 / 제안 이력 s.exec(` CREATE TABLE IF NOT EXISTS input_activity ( id TEXT PRIMARY KEY, date TEXT NOT NULL, hour INTEGER NOT NULL, app_name TEXT NOT NULL DEFAULT '', keystrokes INTEGER NOT NULL DEFAULT 0, shortcuts INTEGER NOT NULL DEFAULT 0, backspaces INTEGER NOT NULL DEFAULT 0, clicks INTEGER NOT NULL DEFAULT 0, double_clicks INTEGER NOT NULL DEFAULT 0, scroll_ticks INTEGER NOT NULL DEFAULT 0, mouse_distance_px INTEGER NOT NULL DEFAULT 0, chars INTEGER NOT NULL DEFAULT 0, words INTEGER NOT NULL DEFAULT 0, sentences INTEGER NOT NULL DEFAULT 0, active_ms INTEGER NOT NULL DEFAULT 0, updated_at INTEGER NOT NULL ); CREATE UNIQUE INDEX IF NOT EXISTS idx_input_activity_bucket ON input_activity(date, hour, app_name); CREATE INDEX IF NOT EXISTS idx_input_activity_date ON input_activity(date); CREATE TABLE IF NOT EXISTS typing_samples ( id TEXT PRIMARY KEY, text TEXT NOT NULL, word_count INTEGER NOT NULL DEFAULT 0, char_count INTEGER NOT NULL DEFAULT 0, app_name TEXT, window_title TEXT, source TEXT NOT NULL DEFAULT 'typed', created_at INTEGER NOT NULL ); CREATE INDEX IF NOT EXISTS idx_typing_samples_created_at ON typing_samples(created_at DESC); CREATE INDEX IF NOT EXISTS idx_typing_samples_source ON typing_samples(source); CREATE TABLE IF NOT EXISTS personal_phrases ( id TEXT PRIMARY KEY, phrase TEXT NOT NULL, count INTEGER NOT NULL DEFAULT 1, source TEXT NOT NULL DEFAULT 'typed', last_used_at INTEGER, created_at INTEGER NOT NULL ); CREATE UNIQUE INDEX IF NOT EXISTS idx_personal_phrases_phrase ON personal_phrases(phrase); CREATE INDEX IF NOT EXISTS idx_personal_phrases_count ON personal_phrases(count DESC); CREATE TABLE IF NOT EXISTS suggestions ( id TEXT PRIMARY KEY, app_name TEXT, prefix_text TEXT NOT NULL DEFAULT '', suggestion_text TEXT NOT NULL, candidate_count INTEGER NOT NULL DEFAULT 1, model TEXT, latency_ms INTEGER, accepted INTEGER NOT NULL DEFAULT 0, created_at INTEGER NOT NULL ); CREATE INDEX IF NOT EXISTS idx_suggestions_created_at ON suggestions(created_at DESC); CREATE INDEX IF NOT EXISTS idx_suggestions_accepted ON suggestions(accepted); CREATE TABLE IF NOT EXISTS phrase_edges ( id TEXT PRIMARY KEY, from_phrase TEXT NOT NULL, to_phrase TEXT NOT NULL, kind TEXT NOT NULL DEFAULT 'follows', weight INTEGER NOT NULL DEFAULT 1, updated_at INTEGER NOT NULL ); CREATE UNIQUE INDEX IF NOT EXISTS idx_phrase_edges_unique ON phrase_edges(from_phrase, to_phrase, kind); CREATE INDEX IF NOT EXISTS idx_phrase_edges_from ON phrase_edges(from_phrase); CREATE INDEX IF NOT EXISTS idx_phrase_edges_weight ON phrase_edges(weight DESC); `) // 개인 그래프: personal_phrases 에 terms/app_name 컬럼 (기존 DB 마이그레이션) try { const phraseCols = s.pragma('table_info(personal_phrases)') as Array<{ name: string }> if (!phraseCols.some((c) => c.name === 'terms')) { s.exec('ALTER TABLE personal_phrases ADD COLUMN terms TEXT') logger.info('Migrated: added terms column to personal_phrases') } if (!phraseCols.some((c) => c.name === 'app_name')) { s.exec('ALTER TABLE personal_phrases ADD COLUMN app_name TEXT') logger.info('Migrated: added app_name column to personal_phrases') } } catch (err) { logger.warn( `personal_phrases graph migration failed: ${err instanceof Error ? err.message : String(err)}` ) } // Phase 14.5: meeting_sessions에 edited_transcript 컬럼 try { const msCols = s.pragma('table_info(meeting_sessions)') as Array<{ name: string }> if (!msCols.some((c) => c.name === 'edited_transcript')) { s.exec('ALTER TABLE meeting_sessions ADD COLUMN edited_transcript TEXT') logger.info('Migrated: added edited_transcript column to meeting_sessions') } } catch (err) { logger.warn( `edited_transcript migration check failed: ${err instanceof Error ? err.message : String(err)}` ) } // Phase 15: history 테이블에 title 컬럼 try { const histCols = s.pragma('table_info(history)') as Array<{ name: string }> if (!histCols.some((c) => c.name === 'title')) { s.exec('ALTER TABLE history ADD COLUMN title TEXT') logger.info('Migrated: added title column to history') } } catch (err) { logger.warn( `title migration check failed: ${err instanceof Error ? err.message : String(err)}` ) } // Phase 12.2: history 테이블에 summary_text 컬럼 try { const columns = s.pragma('table_info(history)') as Array<{ name: string }> const hasSummaryText = columns.some((c) => c.name === 'summary_text') if (!hasSummaryText) { s.exec('ALTER TABLE history ADD COLUMN summary_text TEXT') logger.info('Migrated: added summary_text column to history') } } catch (err) { logger.warn( `summary_text migration check failed: ${err instanceof Error ? err.message : String(err)}` ) } // 기기 간 동기화: 모바일과 같은 필드(즐겨찾기, 메모 수정 시각) + 로컬 outbox/커서 addColumnIfMissing(s, 'history', 'is_favorite', 'INTEGER NOT NULL DEFAULT 0') addColumnIfMissing(s, 'meeting_memos', 'updated_at', 'INTEGER') s.exec(` CREATE TABLE IF NOT EXISTS sync_outbox ( entity TEXT NOT NULL, row_id TEXT NOT NULL, op TEXT NOT NULL, version INTEGER NOT NULL DEFAULT 1, queued_at INTEGER NOT NULL, attempts INTEGER NOT NULL DEFAULT 0, next_attempt_at INTEGER NOT NULL DEFAULT 0, last_error TEXT, PRIMARY KEY (entity, row_id) ); CREATE INDEX IF NOT EXISTS idx_sync_outbox_due ON sync_outbox(next_attempt_at); CREATE TABLE IF NOT EXISTS sync_state ( key TEXT PRIMARY KEY, value TEXT NOT NULL, updated_at INTEGER NOT NULL ); `) } function addColumnIfMissing( s: Database.Database, table: string, column: string, definition: string ): void { try { const columns = s.pragma(`table_info(${table})`) as Array<{ name: string }> if (!columns.some((c) => c.name === column)) { s.exec(`ALTER TABLE ${table} ADD COLUMN ${column} ${definition}`) logger.info(`Migrated: added ${column} column to ${table}`) } } catch (err) { logger.warn( `${table}.${column} migration check failed: ${err instanceof Error ? err.message : String(err)}` ) } } // ───────────────────────────────────────────────────────────── // Public API // ───────────────────────────────────────────────────────────── export interface OpenForUserResult { created: boolean dbPath: string } /** * 사용자별 DB 열기 — userData/users/${userId}/d3ro.db * 이미 다른 사용자 DB가 열려있으면 새 DB 를 연 뒤에 닫는다(실패하면 이전 DB 유지). * 동일 사용자면 no-op. */ export function openForUser(userId: string): OpenForUserResult { if (!userId) { throw new D3ROError(ErrorCode.DBOpenFailed, 'openForUser requires a userId') } if (currentUserId === userId && sqlite && db) { logger.info(`Database already open for user: ${userId}`) return { created: false, dbPath: getUserDbPath(userId) } } // 레거시 DB는 첫 오픈 직전 silent archive archiveLegacyDbIfExists() const dbPath = getUserDbPath(userId) const dirPath = path.dirname(dbPath) const existed = fs.existsSync(dbPath) if (!fs.existsSync(dirPath)) { fs.mkdirSync(dirPath, { recursive: true }) } logger.info(`Opening database for user ${userId} at: ${dbPath} (existed=${existed})`) // 원자적 교체: 새 DB 를 열고 스키마까지 적용한 뒤에야 이전 DB 를 닫는다. // 예전엔 먼저 닫고 열었다 — 계정 DB 가 손상되면(applySchema 실패) 로컬 DB 도 이미 닫혀 앱이 // 재시작 전까지 모든 저장·조회에 실패했다. 실패하면 이전 DB 가 그대로 열려 있다. let next: Database.Database | null = null let nextDb: BetterSQLite3Database try { next = new Database(dbPath) applySchema(next) nextDb = drizzle(next, { schema }) } catch (err) { const message = err instanceof Error ? err.message : String(err) logger.error(`Failed to open database for user ${userId}: ${message} (keeping ${currentUserId ?? 'none'} open)`) try { next?.close() } catch { // ignore } throw new D3ROError(ErrorCode.DBOpenFailed, `Failed to open user DB: ${message}`) } if (sqlite) { closeCurrent() } sqlite = next db = nextDb currentUserId = userId logger.info(`Database opened for user: ${userId}`) // 계정 범위 저장소(명령·템플릿)도 이 사용자로 바꾼다 — 동기화 엔진이 만들어지기 전에 동기적으로 끝난다. emitAccountScopeChanged(userId) return { created: !existed, dbPath } } /** * 익명 로컬 모드 DB 열기. 앱 시작 시 기본으로 호출되는 entry point. * 로그인 없이 바로 동작해야 하는 킬러 피처의 데이터 저장소. * * `userData/users/_local/d3ro.db`에 저장. 로그아웃 복귀 시 같은 경로로 재오픈. */ export function openLocal(): OpenForUserResult { return openForUser(LOCAL_USER_ID) } /** * 현재 열려있는 DB 닫기. 아무 것도 안 열려 있으면 no-op. */ export function closeCurrent(): void { if (!sqlite) return const prevUserId = currentUserId try { sqlite.close() } catch (err) { logger.warn( `Database close error: ${err instanceof Error ? err.message : String(err)}` ) } sqlite = null db = null currentUserId = null logger.info(`Database closed (user was: ${prevUserId ?? 'none'})`) } export function getDatabase(): BetterSQLite3Database { if (!db) { throw new D3ROError( ErrorCode.DBOpenFailed, 'Database not initialized. Sign in first (openForUser).' ) } return db } export function getCurrentUserId(): string | null { return currentUserId } export function isDatabaseOpen(): boolean { return db !== null } // ───────────────────────────────────────────────────────────── // 익명 로컬 데이터 → 계정 DB 가져오기 // ───────────────────────────────────────────────────────────── /** 가져올 테이블. 입력 텔레메트리·RAG 색인 등 기기 전용 데이터는 옮기지 않는다. */ const IMPORTABLE_TABLES = [ 'history', 'dictionary', 'memo_tags', 'meeting_sessions', 'meeting_memos', 'meeting_documents', ] as const const LOCAL_CLAIM_KEY = 'claimedBy' const IMPORT_DONE_KEY = 'localImport:v1' export interface LocalImportResult { imported: number tables: Record } function tableColumns(s: Database.Database, schemaName: string, table: string): string[] { const rows = s.pragma(`${schemaName}.table_info(${table})`) as Array<{ name: string }> return rows.map((r) => r.name) } /** * 로그인 전(익명 로컬 모드)에 쌓인 기록을 현재 열린 계정 DB로 복사한다. * - 계정 DB마다 한 번만(sync_state 플래그), 익명 DB는 처음 가져간 계정 하나만(claimedBy) 가져간다. * - 원본은 지우지 않는다. 같은 id·같은 단어는 건너뛴다(INSERT OR IGNORE). * - 옛 DB는 컬럼 순서가 다를 수 있어 양쪽에 모두 있는 컬럼만 이름으로 복사한다. * 가져온 행은 이후 동기화의 최초 대조(backfill)가 서버로 올린다. */ export function importLocalModeData(): LocalImportResult | null { if (!sqlite || !currentUserId || currentUserId === LOCAL_USER_ID) return null const s = sqlite const userId = currentUserId const done = s.prepare('SELECT value FROM sync_state WHERE key = ?').get(IMPORT_DONE_KEY) as | { value: string } | undefined if (done) return null const localPath = getUserDbPath(LOCAL_USER_ID) const markDone = (): void => { s.prepare( 'INSERT OR REPLACE INTO sync_state(key, value, updated_at) VALUES (?, ?, ?)' ).run(IMPORT_DONE_KEY, String(Date.now()), Date.now()) } if (!fs.existsSync(localPath)) { markDone() return null } s.prepare('ATTACH DATABASE ? AS anon').run(localPath) try { s.exec(`CREATE TABLE IF NOT EXISTS anon.sync_state ( key TEXT PRIMARY KEY, value TEXT NOT NULL, updated_at INTEGER NOT NULL )`) const claim = s.prepare('SELECT value FROM anon.sync_state WHERE key = ?').get(LOCAL_CLAIM_KEY) as | { value: string } | undefined if (claim && claim.value !== userId) { logger.info(`Local-mode data already imported by another account; skipping for ${userId}`) markDone() return null } const result: LocalImportResult = { imported: 0, tables: {} } const copy = s.transaction(() => { for (const table of IMPORTABLE_TABLES) { const sourceColumns = tableColumns(s, 'anon', table) if (sourceColumns.length === 0) continue const targetColumns = new Set(tableColumns(s, 'main', table)) const columns = sourceColumns.filter((c) => targetColumns.has(c)).map((c) => `"${c}"`) if (columns.length === 0) continue const list = columns.join(', ') const info = s .prepare(`INSERT OR IGNORE INTO main.${table} (${list}) SELECT ${list} FROM anon.${table}`) .run() result.tables[table] = info.changes result.imported += info.changes } s.prepare( 'INSERT OR REPLACE INTO anon.sync_state(key, value, updated_at) VALUES (?, ?, ?)' ).run(LOCAL_CLAIM_KEY, userId, Date.now()) markDone() }) copy() logger.info(`Imported ${result.imported} local-mode row(s) into ${userId}: ${JSON.stringify(result.tables)}`) return result } finally { s.exec('DETACH DATABASE anon') } } /** * 테스트/개발용 — 현재 오픈된 DB 경로 조회. */ export function getCurrentDbPath(): string | null { return currentUserId ? getUserDbPath(currentUserId) : null } /** * Vitest 전용: in-memory drizzle DB를 서비스들이 쓰는 getDatabase()에 연결한다. * 파일 I/O / Electron userData 없이 History·Dictionary·Meeting 등을 실제 쿼리로 검증한다. */ export function bindTestDatabase( testDb: BetterSQLite3Database, userId: string = LOCAL_USER_ID, ): void { db = testDb currentUserId = userId emitAccountScopeChanged(userId) } /** Vitest 전용: bindTestDatabase 해제. */ export function unbindTestDatabase(): void { db = null currentUserId = null }