diff --git a/apps/desktop/src/main/services/DictionaryService.ts b/apps/desktop/src/main/services/DictionaryService.ts index cc1c1bc..167c98c 100644 --- a/apps/desktop/src/main/services/DictionaryService.ts +++ b/apps/desktop/src/main/services/DictionaryService.ts @@ -1,7 +1,7 @@ // src/main/services/DictionaryService.ts // 사용자 커스텀 단어 사전. 설계서 01/03 IDictionaryService 구현. -import { eq, and, desc, count, sql } from 'drizzle-orm' +import { eq, desc, count, sql } from 'drizzle-orm' import path from 'path' import fs from 'fs' import { app, dialog } from 'electron' @@ -64,19 +64,17 @@ function findCaseInsensitiveDuplicate( } /** - * 가져오기용 한 번 조회 — 대량 행을 JS 로 비교하지 않고 SQL 에서 (lower(trim(word)), category) 로 찾는다. - * SQLite lower() 는 ASCII 만 접으므로 비 ASCII 대소문자 변형은 추가·수정 경로(JS 비교)만큼 엄밀하지 않다. + * 가져오기용 식별 키 집합 `${category}|${dictionaryIdentityKey(word)}` — 가져오기 시작 때 한 번 읽는다. + * SQL lower() 는 ASCII 만 접어 'Äpfel'/'äpfel' 이 따로 들어갔고, 동기화 때 서버 유일 인덱스와 부딪혀 + * 한쪽 표기가 조용히 덮였다. 추가·수정 경로와 같은 JS 기준으로 비교한다. */ -function findImportDuplicate( - db: DictionaryDb, - word: string, - category: DictionaryEntry['category'] -): { id: string } | undefined { - return db - .select({ id: dictionary.id }) - .from(dictionary) - .where(and(sql`lower(trim(${dictionary.word})) = ${dictionaryIdentityKey(word)}`, eq(dictionary.category, category))) - .get() +function importIdentityKey(word: string, category: DictionaryEntry['category']): string { + return `${category}|${dictionaryIdentityKey(word)}` +} + +function loadImportIdentities(db: DictionaryDb): Set { + const rows = db.select({ word: dictionary.word, category: dictionary.category }).from(dictionary).all() + return new Set(rows.map((r) => importIdentityKey(r.word, r.category))) } class DictionaryService { @@ -332,6 +330,7 @@ class DictionaryService { const outcome: DictionaryImportResult = { imported: 0, skipped: 0, errors: 0 } db.transaction((tx) => { + const known = loadImportIdentities(tx) for (const record of parsed) { // 빈 단어·서버 길이 제한 초과 행은 오류로 센다 — 로컬에만 남고 동기화되지 않는 항목을 만들지 않는다 const candidate = toDictionaryImportRow(record, now) @@ -341,8 +340,8 @@ class DictionaryService { } const { row } = candidate // 대소문자만 다른 단어도 같은 단어로 본다(서버 유일성과 같은 기준) - const existing = findImportDuplicate(tx, row.word, row.category) - if (existing) { + const key = importIdentityKey(row.word, row.category) + if (known.has(key)) { outcome.skipped += 1 continue } @@ -353,6 +352,7 @@ class DictionaryService { .values({ id, ...row }) .run() outcome.imported += 1 + known.add(key) void getCloudSyncService().pushOne('dictionary', id) } catch { outcome.skipped += 1 diff --git a/apps/desktop/tests/main/services/DictionaryIo.test.ts b/apps/desktop/tests/main/services/DictionaryIo.test.ts index 25e1fdb..212e9d5 100644 --- a/apps/desktop/tests/main/services/DictionaryIo.test.ts +++ b/apps/desktop/tests/main/services/DictionaryIo.test.ts @@ -35,7 +35,7 @@ interface TxStub { insert: ReturnType } -let existingRow: unknown = null +let existingRows: Array<{ word: string; category: string }> = [] let txStub: TxStub const mockDb = { @@ -67,10 +67,10 @@ let mod: ServiceModule beforeEach(async () => { vi.clearAllMocks() - existingRow = null + existingRows = [] txStub = { select: vi.fn(() => ({ - from: vi.fn(() => ({ where: vi.fn(() => ({ get: vi.fn(() => existingRow) })) })) + from: vi.fn(() => ({ all: vi.fn(() => existingRows) })) })), insert: vi.fn(() => ({ values: vi.fn(() => ({ run: vi.fn(() => ({ changes: 1 })) })) })) } @@ -98,7 +98,7 @@ describe('DictionaryService.importDictionary', () => { }) it('skips entries that already exist', () => { - existingRow = { id: 'existing' } + existingRows = [{ word: 'AI', category: 'user' }] readFileSync.mockReturnValue(JSON.stringify({ entries: [{ word: 'AI' }] })) const result = mod.getDictionaryService().importDictionary({ @@ -109,6 +109,18 @@ describe('DictionaryService.importDictionary', () => { expect(result).toEqual({ imported: 0, skipped: 1, errors: 0 }) }) + it('treats non-ASCII case variants as the same word (Äpfel = äpfel) and dedupes within the file', () => { + existingRows = [{ word: 'Äpfel', category: 'user' }] + readFileSync.mockReturnValue(JSON.stringify({ entries: [{ word: 'äpfel' }, { word: 'Éclair' }, { word: 'éclair' }] })) + + const result = mod.getDictionaryService().importDictionary({ + filePath: '/in/dict.json', + format: 'json' + }) + + expect(result).toEqual({ imported: 1, skipped: 2, errors: 0 }) + }) + it('parses CSV exports', () => { readFileSync.mockReturnValue( 'word,pronunciation,category,usageCount,createdAt,updatedAt\r\n"테스트",,"user",0,100,200\r\n'