1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
/*
* Copyright (C) 2023-2024 Yomitan Authors
* Copyright (C) 2020-2022 Yomichan Authors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {IDBFactory, IDBKeyRange} from 'fake-indexeddb';
import {readFileSync} from 'node:fs';
import {fileURLToPath} from 'node:url';
import {join, dirname as pathDirname} from 'path';
import {beforeEach, describe, test, vi} from 'vitest';
import {createDictionaryArchiveData, getDictionaryArchiveIndex} from '../dev/dictionary-archive-util.js';
import {parseJson} from '../dev/json.js';
import {DictionaryDatabase} from '../ext/js/dictionary/dictionary-database.js';
import {DictionaryImporter} from '../ext/js/dictionary/dictionary-importer.js';
import {DictionaryImporterMediaLoader} from './mocks/dictionary-importer-media-loader.js';
const dirname = pathDirname(fileURLToPath(import.meta.url));
vi.stubGlobal('IDBKeyRange', IDBKeyRange);
/**
* @param {string} dictionary
* @param {string} [dictionaryName]
* @returns {Promise<ArrayBuffer>}
*/
async function createTestDictionaryArchiveData(dictionary, dictionaryName) {
const dictionaryDirectory = join(dirname, 'data', 'dictionaries', dictionary);
return await createDictionaryArchiveData(dictionaryDirectory, dictionaryName);
}
/**
* @param {import('vitest').ExpectStatic} expect
* @param {import('dictionary-importer').OnProgressCallback} [onProgress]
* @returns {DictionaryImporter}
*/
function createDictionaryImporter(expect, onProgress) {
const dictionaryImporterMediaLoader = new DictionaryImporterMediaLoader();
return new DictionaryImporter(dictionaryImporterMediaLoader, (...args) => {
const {stepIndex, stepCount, index, count} = args[0];
expect.soft(stepIndex < stepCount).toBe(true);
expect.soft(index <= count).toBe(true);
if (typeof onProgress === 'function') {
onProgress(...args);
}
});
}
/**
* @param {import('dictionary-database').TermEntry[]} dictionaryDatabaseEntries
* @param {string} term
* @returns {number}
*/
function countDictionaryDatabaseEntriesWithTerm(dictionaryDatabaseEntries, term) {
return dictionaryDatabaseEntries.reduce((i, v) => (i + (v.term === term ? 1 : 0)), 0);
}
/**
* @param {import('dictionary-database').TermEntry[]} dictionaryDatabaseEntries
* @param {string} reading
* @returns {number}
*/
function countDictionaryDatabaseEntriesWithReading(dictionaryDatabaseEntries, reading) {
return dictionaryDatabaseEntries.reduce((i, v) => (i + (v.reading === reading ? 1 : 0)), 0);
}
/**
* @param {import('dictionary-database').TermMeta[]|import('dictionary-database').KanjiMeta[]} metas
* @param {import('dictionary-database').TermMetaType|import('dictionary-database').KanjiMetaType} mode
* @returns {number}
*/
function countMetasWithMode(metas, mode) {
let i = 0;
for (const item of metas) {
if (item.mode === mode) { ++i; }
}
return i;
}
/**
* @param {import('dictionary-database').KanjiEntry[]} kanji
* @param {string} character
* @returns {number}
*/
function countKanjiWithCharacter(kanji, character) {
let i = 0;
for (const item of kanji) {
if (item.character === character) { ++i; }
}
return i;
}
/** */
describe('Database', () => {
beforeEach(async () => {
globalThis.indexedDB = new IDBFactory();
});
test('Database invalid usage', async ({expect}) => {
// Load dictionary data
const testDictionarySource = await createTestDictionaryArchiveData('valid-dictionary1');
const testDictionaryIndex = await getDictionaryArchiveIndex(testDictionarySource);
const title = testDictionaryIndex.title;
const titles = new Map([
[title, {priority: 0, allowSecondarySearches: false}],
]);
// Setup database
const dictionaryDatabase = new DictionaryDatabase();
/** @type {import('dictionary-importer').ImportDetails} */
const defaultImportDetails = {prefixWildcardsSupported: false};
// Database not open
await expect.soft(dictionaryDatabase.deleteDictionary(title, 1000, () => {})).rejects.toThrow('Database not open');
await expect.soft(dictionaryDatabase.findTermsBulk(['?'], titles, 'exact')).rejects.toThrow('Database not open');
await expect.soft(dictionaryDatabase.findTermsExactBulk([{term: '?', reading: '?'}], titles)).rejects.toThrow('Database not open');
await expect.soft(dictionaryDatabase.findTermsBySequenceBulk([{query: 1, dictionary: title}])).rejects.toThrow('Database not open');
await expect.soft(dictionaryDatabase.findTermMetaBulk(['?'], titles)).rejects.toThrow('Database not open');
await expect.soft(dictionaryDatabase.findTermMetaBulk(['?'], titles)).rejects.toThrow('Database not open');
await expect.soft(dictionaryDatabase.findKanjiBulk(['?'], titles)).rejects.toThrow('Database not open');
await expect.soft(dictionaryDatabase.findKanjiMetaBulk(['?'], titles)).rejects.toThrow('Database not open');
await expect.soft(dictionaryDatabase.findTagForTitle('tag', title)).rejects.toThrow('Database not open');
await expect.soft(dictionaryDatabase.getDictionaryInfo()).rejects.toThrow('Database not open');
await expect.soft(dictionaryDatabase.getDictionaryCounts([...titles.keys()], true)).rejects.toThrow('Database not open');
await expect.soft(createDictionaryImporter(expect).importDictionary(dictionaryDatabase, testDictionarySource, defaultImportDetails)).rejects.toThrow('Database is not ready');
await dictionaryDatabase.prepare();
// Already prepared
await expect.soft(dictionaryDatabase.prepare()).rejects.toThrow('Database already open');
await createDictionaryImporter(expect).importDictionary(dictionaryDatabase, testDictionarySource, defaultImportDetails);
// Dictionary already imported
expect.soft(await createDictionaryImporter(expect).importDictionary(dictionaryDatabase, testDictionarySource, defaultImportDetails)).toEqual({result: null, errors: [new Error('Dictionary Test Dictionary is already imported, skipped it.')]});
await dictionaryDatabase.close();
});
describe('Invalid dictionaries', () => {
const invalidDictionaries = [
{name: 'invalid-dictionary1'},
{name: 'invalid-dictionary2'},
{name: 'invalid-dictionary3'},
{name: 'invalid-dictionary4'},
{name: 'invalid-dictionary5'},
{name: 'invalid-dictionary6'},
];
describe.each(invalidDictionaries)('Invalid dictionary: $name', ({name}) => {
test('Has invalid data', async ({expect}) => {
const dictionaryDatabase = new DictionaryDatabase();
await dictionaryDatabase.prepare();
const testDictionarySource = await createTestDictionaryArchiveData(name);
/** @type {import('dictionary-importer').ImportDetails} */
const detaultImportDetails = {prefixWildcardsSupported: false};
await expect.soft(createDictionaryImporter(expect).importDictionary(dictionaryDatabase, testDictionarySource, detaultImportDetails)).rejects.toThrow('Dictionary has invalid data');
await dictionaryDatabase.close();
});
});
});
describe('Database valid usage', () => {
const testDataFilePath = join(dirname, 'data/database-test-cases.json');
/** @type {import('test/database').DatabaseTestData} */
const testData = parseJson(readFileSync(testDataFilePath, {encoding: 'utf8'}));
test('Import data and test', async ({expect}) => {
const fakeImportDate = testData.expectedSummary.importDate;
// Load dictionary data
const testDictionarySource = await createTestDictionaryArchiveData('valid-dictionary1');
const testDictionaryIndex = await getDictionaryArchiveIndex(testDictionarySource);
const title = testDictionaryIndex.title;
const titles = new Map([
[title, {priority: 0, allowSecondarySearches: false}],
]);
// Setup database
const dictionaryDatabase = new DictionaryDatabase();
await dictionaryDatabase.prepare();
// Import data
let progressEvent1 = false;
const dictionaryImporter = createDictionaryImporter(expect, () => { progressEvent1 = true; });
const {result: importDictionaryResult, errors: importDictionaryErrors} = await dictionaryImporter.importDictionary(
dictionaryDatabase,
testDictionarySource,
{prefixWildcardsSupported: true},
);
if (importDictionaryResult) {
importDictionaryResult.importDate = fakeImportDate;
}
expect.soft(importDictionaryErrors).toStrictEqual([]);
expect.soft(importDictionaryResult).toStrictEqual(testData.expectedSummary);
expect.soft(progressEvent1).toBe(true);
// Get info summary
const info = await dictionaryDatabase.getDictionaryInfo();
for (const item of info) { item.importDate = fakeImportDate; }
expect.soft(info).toStrictEqual([testData.expectedSummary]);
// Get counts
const counts = await dictionaryDatabase.getDictionaryCounts(info.map((v) => v.title), true);
expect.soft(counts).toStrictEqual(testData.expectedCounts);
// Test findTermsBulk
for (const {inputs, expectedResults} of testData.tests.findTermsBulk) {
for (const {termList, matchType} of inputs) {
const results = await dictionaryDatabase.findTermsBulk(termList, titles, matchType);
expect.soft(results.length).toStrictEqual(expectedResults.total);
for (const [term, count] of expectedResults.terms) {
expect.soft(countDictionaryDatabaseEntriesWithTerm(results, term)).toStrictEqual(count);
}
for (const [reading, count] of expectedResults.readings) {
expect.soft(countDictionaryDatabaseEntriesWithReading(results, reading)).toStrictEqual(count);
}
}
}
// Test findTermsExactBulk
for (const {inputs, expectedResults} of testData.tests.findTermsExactBulk) {
for (const {termList} of inputs) {
const results = await dictionaryDatabase.findTermsExactBulk(termList, titles);
expect.soft(results.length).toStrictEqual(expectedResults.total);
for (const [term, count] of expectedResults.terms) {
expect.soft(countDictionaryDatabaseEntriesWithTerm(results, term)).toStrictEqual(count);
}
for (const [reading, count] of expectedResults.readings) {
expect.soft(countDictionaryDatabaseEntriesWithReading(results, reading)).toStrictEqual(count);
}
}
}
// Test findTermsBySequenceBulk
for (const {inputs, expectedResults} of testData.tests.findTermsBySequenceBulk) {
for (const {sequenceList} of inputs) {
const results = await dictionaryDatabase.findTermsBySequenceBulk(sequenceList.map((query) => ({query, dictionary: title})));
expect.soft(results.length).toStrictEqual(expectedResults.total);
for (const [term, count] of expectedResults.terms) {
expect.soft(countDictionaryDatabaseEntriesWithTerm(results, term)).toStrictEqual(count);
}
for (const [reading, count] of expectedResults.readings) {
expect.soft(countDictionaryDatabaseEntriesWithReading(results, reading)).toStrictEqual(count);
}
}
}
// Test findTermMetaBulk
for (const {inputs, expectedResults} of testData.tests.findTermMetaBulk) {
for (const {termList} of inputs) {
const results = await dictionaryDatabase.findTermMetaBulk(termList, titles);
expect.soft(results.length).toStrictEqual(expectedResults.total);
for (const [mode, count] of expectedResults.modes) {
expect.soft(countMetasWithMode(results, mode)).toStrictEqual(count);
}
}
}
// Test findKanjiBulk
for (const {inputs, expectedResults} of testData.tests.findKanjiBulk) {
for (const {kanjiList} of inputs) {
const results = await dictionaryDatabase.findKanjiBulk(kanjiList, titles);
expect.soft(results.length).toStrictEqual(expectedResults.total);
for (const [kanji, count] of expectedResults.kanji) {
expect.soft(countKanjiWithCharacter(results, kanji)).toStrictEqual(count);
}
}
}
// Test findKanjiBulk
for (const {inputs, expectedResults} of testData.tests.findKanjiMetaBulk) {
for (const {kanjiList} of inputs) {
const results = await dictionaryDatabase.findKanjiMetaBulk(kanjiList, titles);
expect.soft(results.length).toStrictEqual(expectedResults.total);
for (const [mode, count] of expectedResults.modes) {
expect.soft(countMetasWithMode(results, mode)).toStrictEqual(count);
}
}
}
// Test findTagForTitle
for (const {inputs, expectedResults} of testData.tests.findTagForTitle) {
for (const {name} of inputs) {
const result = await dictionaryDatabase.findTagForTitle(name, title);
expect.soft(result).toStrictEqual(expectedResults.value);
}
}
// Close
await dictionaryDatabase.close();
});
});
describe('Database cleanup', () => {
/** @type {{clearMethod: 'purge'|'delete'}[]} */
const cleanupTestCases = [
{clearMethod: 'purge'},
{clearMethod: 'delete'},
];
describe.each(cleanupTestCases)('Testing cleanup method $clearMethod', ({clearMethod}) => {
test('Import data and test', async ({expect}) => {
// Load dictionary data
const testDictionarySource = await createTestDictionaryArchiveData('valid-dictionary1');
const testDictionaryIndex = await getDictionaryArchiveIndex(testDictionarySource);
// Setup database
const dictionaryDatabase = new DictionaryDatabase();
await dictionaryDatabase.prepare();
// Import data
const dictionaryImporter = createDictionaryImporter(expect);
await dictionaryImporter.importDictionary(dictionaryDatabase, testDictionarySource, {prefixWildcardsSupported: true});
// Clear
switch (clearMethod) {
case 'purge':
await dictionaryDatabase.purge();
break;
case 'delete':
{
let progressEvent2 = false;
await dictionaryDatabase.deleteDictionary(
testDictionaryIndex.title,
1000,
() => { progressEvent2 = true; },
);
expect(progressEvent2).toBe(true);
}
break;
}
// Test empty
const info = await dictionaryDatabase.getDictionaryInfo();
expect.soft(info).toStrictEqual([]);
const counts = await dictionaryDatabase.getDictionaryCounts([], true);
/** @type {import('dictionary-database').DictionaryCounts} */
const countsExpected = {
counts: [],
total: {kanji: 0, kanjiMeta: 0, terms: 0, termMeta: 0, tagMeta: 0, media: 0},
};
expect.soft(counts).toStrictEqual(countsExpected);
// Close
await dictionaryDatabase.close();
});
});
});
});
|