246 lines
8.8 KiB
TypeScript
246 lines
8.8 KiB
TypeScript
// tests/main/db/sqlite-optimizations.test.ts
|
|
// SQLite 4대 수술 검증:
|
|
// 1) WAL 성능 PRAGMA (synchronous=NORMAL, temp_store=MEMORY, cache_size=-16000)
|
|
// 2) FTS5 trigram 가상 테이블 + INSERT/UPDATE/DELETE 트리거 + UnifiedSearchService 연동
|
|
// 3) RAG Float32 바이너리 BLOB + 메모리 캐시 (JSON.parse 0회)
|
|
// 4) PersonalGraphService 단일 트랜잭션 일괄 반영
|
|
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import { eq } from 'drizzle-orm'
|
|
import { createTestDb } from '../../helpers/createTestDb'
|
|
import { bindTestDatabase, unbindTestDatabase } from '../../../src/main/db'
|
|
import {
|
|
history,
|
|
meetingMemos,
|
|
meetingSessions,
|
|
ragChunks,
|
|
} from '../../../src/main/db/schema'
|
|
import {
|
|
getUnifiedSearchService,
|
|
resetUnifiedSearchServiceForTests,
|
|
} from '../../../src/main/services/UnifiedSearchService'
|
|
import { SqliteChunkStore } from '../../../src/main/services/rag/chunk-store'
|
|
import { rankChunks } from '../../../src/main/services/rag/retrieval'
|
|
import {
|
|
getPersonalGraphService,
|
|
resetPersonalGraphServiceForTests,
|
|
} from '../../../src/main/services/PersonalGraphService'
|
|
|
|
const BASE_TS = 1_730_000_000_000
|
|
|
|
describe('SQLite 성능 및 검색/벡터 최적화', () => {
|
|
let testDb: ReturnType<typeof createTestDb>
|
|
|
|
beforeEach(() => {
|
|
testDb = createTestDb()
|
|
bindTestDatabase(testDb.db)
|
|
resetUnifiedSearchServiceForTests()
|
|
resetPersonalGraphServiceForTests()
|
|
})
|
|
|
|
afterEach(() => {
|
|
resetUnifiedSearchServiceForTests()
|
|
resetPersonalGraphServiceForTests()
|
|
unbindTestDatabase()
|
|
testDb.close()
|
|
vi.restoreAllMocks()
|
|
})
|
|
|
|
it('applySchema_실행시_WAL성능프라그마와FTS5트리그램테이블을설정한다', () => {
|
|
const synchronous = testDb.sqlite.pragma('synchronous', { simple: true })
|
|
const tempStore = testDb.sqlite.pragma('temp_store', { simple: true })
|
|
const cacheSize = testDb.sqlite.pragma('cache_size', { simple: true })
|
|
|
|
// synchronous: 1 = NORMAL, temp_store: 2 = MEMORY, cache_size: -16000 (~16MB)
|
|
expect(synchronous).toBe(1)
|
|
expect(tempStore).toBe(2)
|
|
expect(cacheSize).toBe(-16000)
|
|
|
|
const chunkColumns = testDb.sqlite.pragma('table_info(rag_chunks)') as Array<{ name: string }>
|
|
expect(chunkColumns.map((col) => col.name)).toContain('embedding_blob')
|
|
|
|
const ftsTables = testDb.sqlite
|
|
.prepare(
|
|
`SELECT name FROM sqlite_master WHERE type = 'table' AND name IN ('history_fts', 'meeting_sessions_fts', 'meeting_memos_fts') ORDER BY name`
|
|
)
|
|
.all() as Array<{ name: string }>
|
|
expect(ftsTables.map((row) => row.name)).toEqual([
|
|
'history_fts',
|
|
'meeting_memos_fts',
|
|
'meeting_sessions_fts',
|
|
])
|
|
})
|
|
|
|
it('UnifiedSearchService_3자이상검색시_FTS5인덱스를타고수정삭제와동기화된다', () => {
|
|
testDb.db
|
|
.insert(history)
|
|
.values({
|
|
id: 'rec-1',
|
|
title: '주간 아키텍처 점검',
|
|
originalText: '초안 텍스트',
|
|
polishedText: '데이터베이스 인덱싱 최적화 방안을 논의했습니다.',
|
|
summaryText: '인덱싱 요약',
|
|
duration: 12.5,
|
|
createdAt: BASE_TS + 100,
|
|
updatedAt: BASE_TS + 100,
|
|
})
|
|
.run()
|
|
|
|
testDb.db
|
|
.insert(meetingSessions)
|
|
.values({
|
|
id: 'mtg-1',
|
|
title: '스프린트 회고',
|
|
status: 'completed',
|
|
startedAt: BASE_TS + 200,
|
|
rawTranscript: '[00:01] 백엔드 인덱싱 지연을 줄였습니다.',
|
|
createdAt: BASE_TS + 200,
|
|
updatedAt: BASE_TS + 200,
|
|
})
|
|
.run()
|
|
|
|
testDb.db
|
|
.insert(meetingSessions)
|
|
.values({
|
|
id: 'mtg-2',
|
|
title: '배포 체크리스트',
|
|
status: 'completed',
|
|
startedAt: BASE_TS + 300,
|
|
rawTranscript: '[00:05] 릴리스 일정 공유',
|
|
createdAt: BASE_TS + 300,
|
|
updatedAt: BASE_TS + 300,
|
|
})
|
|
.run()
|
|
|
|
testDb.db
|
|
.insert(meetingMemos)
|
|
.values({
|
|
id: 'memo-1',
|
|
sessionId: 'mtg-2',
|
|
content: '메모에만 적힌 인덱싱 확인 항목',
|
|
timestampMs: 5_000,
|
|
createdAt: BASE_TS + 310,
|
|
})
|
|
.run()
|
|
|
|
// EXPLAIN QUERY PLAN 검증: 전체 테이블 스캔 대신 history_fts 가상 테이블 인덱스를 타야 한다.
|
|
const planRows = testDb.sqlite
|
|
.prepare(
|
|
`EXPLAIN QUERY PLAN SELECT id FROM history WHERE id IN (SELECT row_id FROM history_fts WHERE history_fts MATCH '"인덱싱"')`
|
|
)
|
|
.all() as Array<{ detail: string }>
|
|
expect(planRows.some((row) => row.detail.includes('history_fts'))).toBe(true)
|
|
expect(planRows.some((row) => row.detail.includes('SCAN history '))).toBe(false)
|
|
|
|
const initial = getUnifiedSearchService().search({ query: '인덱싱' })
|
|
expect(initial.total).toBe(3)
|
|
expect(initial.results.map((item) => item.id)).toEqual(['mtg-2', 'mtg-1', 'rec-1'])
|
|
|
|
// UPDATE 트리거 동기화 검증
|
|
testDb.db
|
|
.update(history)
|
|
.set({
|
|
polishedText: '쿼리 플랜 개편안을 확정했습니다 (100% 달성).',
|
|
summaryText: '개편안 요약',
|
|
updatedAt: BASE_TS + 400,
|
|
})
|
|
.where(eq(history.id, 'rec-1'))
|
|
.run()
|
|
|
|
// DELETE 트리거 동기화 검증
|
|
testDb.db.delete(meetingMemos).where(eq(meetingMemos.id, 'memo-1')).run()
|
|
|
|
const afterMutation = getUnifiedSearchService().search({ query: '인덱싱' })
|
|
expect(afterMutation.results.map((item) => item.id)).toEqual(['mtg-1'])
|
|
|
|
// 리터럴 % 와일드카드 및 2자 이하 짧은 검색어도 정확히 일치해야 한다.
|
|
const literalPercent = getUnifiedSearchService().search({ query: '100% 달성' })
|
|
expect(literalPercent.results.map((item) => item.id)).toEqual(['rec-1'])
|
|
|
|
const shortQuery = getUnifiedSearchService().search({ query: '회고' })
|
|
expect(shortQuery.results.map((item) => item.id)).toEqual(['mtg-1'])
|
|
})
|
|
|
|
it('SqliteChunkStore_임베딩저장시_Float32바이너리와메모리캐시로JSON재파싱없이검색한다', () => {
|
|
const store = new SqliteChunkStore()
|
|
store.insertDocument({
|
|
id: 'doc-1',
|
|
fileName: 'architecture.md',
|
|
filePath: '/docs/architecture.md',
|
|
fileType: 'md',
|
|
chunkCount: 2,
|
|
indexed: true,
|
|
indexedAt: BASE_TS,
|
|
addedAt: BASE_TS,
|
|
})
|
|
store.replaceChunks('doc-1', ['SQLite WAL 모드 설명', '벡터 임베딩 바이너리 직렬화'])
|
|
|
|
const chunks = store.listChunks('doc-1')
|
|
expect(chunks).toHaveLength(2)
|
|
|
|
store.setEmbedding(chunks[0].id, [1, 0, 0])
|
|
store.setEmbedding(chunks[1].id, [0, 1, 0])
|
|
|
|
const rawRow = testDb.db
|
|
.select()
|
|
.from(ragChunks)
|
|
.where(eq(ragChunks.id, chunks[0].id))
|
|
.get()
|
|
expect(rawRow?.embeddingBlob?.byteLength).toBe(12)
|
|
|
|
const parseSpy = vi.spyOn(JSON, 'parse')
|
|
const embeddedFirst = store.listEmbeddedChunks()
|
|
expect(embeddedFirst).toHaveLength(2)
|
|
|
|
const ranked = rankChunks(
|
|
[0.05, 0.99, 0],
|
|
embeddedFirst,
|
|
new Map([['doc-1', 'architecture.md']]),
|
|
2,
|
|
)
|
|
expect(ranked[0].content).toBe('벡터 임베딩 바이너리 직렬화')
|
|
expect(parseSpy).not.toHaveBeenCalled()
|
|
|
|
// 두 번째 조회는 메모리 캐시를 그대로 반환하고, replaceChunks/removeDocument 시 즉시 무효화한다.
|
|
const selectSpy = vi.spyOn(testDb.db, 'select')
|
|
const embeddedCached = store.listEmbeddedChunks()
|
|
expect(embeddedCached).toHaveLength(2)
|
|
expect(selectSpy).not.toHaveBeenCalled()
|
|
|
|
expect(store.counts()).toEqual({ documentCount: 1, totalChunks: 2 })
|
|
|
|
store.removeDocument('doc-1')
|
|
expect(store.listEmbeddedChunks()).toEqual([])
|
|
expect(store.counts()).toEqual({ documentCount: 0, totalChunks: 0 })
|
|
})
|
|
|
|
it('PersonalGraphService_다중문장색인중중간실패시_단일트랜잭션으로전체롤백하고정상시일괄반영한다', () => {
|
|
const graph = getPersonalGraphService()
|
|
|
|
// phrase_edges 쓰기 단계에서 실패를 유도해 앞선 personal_phrases INSERT 까지 원자적으로 롤백되는지 검증한다.
|
|
testDb.sqlite.exec(`
|
|
CREATE TRIGGER fail_edge_insert BEFORE INSERT ON phrase_edges
|
|
BEGIN
|
|
SELECT RAISE(ABORT, 'simulated_edge_failure');
|
|
END;
|
|
`)
|
|
|
|
graph.indexText(
|
|
'배포 파이프라인 성능 점검 결과를 공유드립니다. 배포 파이프라인 성능 점검 지표를 확인했습니다.',
|
|
{ source: 'typed', appName: 'Slack.exe', at: BASE_TS },
|
|
)
|
|
expect(graph.getStats(3650).nodes).toBe(0)
|
|
|
|
testDb.sqlite.exec('DROP TRIGGER fail_edge_insert;')
|
|
|
|
graph.indexText(
|
|
'배포 파이프라인 성능 점검 결과를 공유드립니다. 배포 파이프라인 성능 점검 지표를 확인했습니다.',
|
|
{ source: 'typed', appName: 'Slack.exe', at: BASE_TS },
|
|
)
|
|
|
|
const stats = graph.getStats(3650)
|
|
expect(stats.nodes).toBe(2)
|
|
expect(stats.followsEdges).toBe(1)
|
|
expect(stats.sharesTermsEdges).toBe(1)
|
|
})
|
|
})
|