fix(dictionary): catch non-ASCII case duplicates when importing a dictionary file
SQLite lower() only folds ASCII, so importing 'äpfel' next to an existing 'Äpfel' created a second local entry that later collided with the server's unique index and silently overwrote one spelling. Import now compares with the same JS identity key as add/update, including duplicates within the file.
This commit is contained in:
parent
c3ca95d20f
commit
4e75f515e2
2 changed files with 31 additions and 19 deletions
|
|
@ -35,7 +35,7 @@ interface TxStub {
|
|||
insert: ReturnType<typeof vi.fn>
|
||||
}
|
||||
|
||||
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'
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue