Pop-up dictionary browser extension for language learning. Successor to Yomichan. (PERSONAL FORK)
1/*
2 * Copyright (C) 2023-2025 Yomitan Authors
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18import {readFileSync} from 'fs';
19import {fileURLToPath} from 'node:url';
20import path from 'path';
21import {describe} from 'vitest';
22import {parseJson} from '../dev/json.js';
23import {createTranslatorTest} from './fixtures/translator-test.js';
24import {createTestAnkiNoteData, getTemplateRenderResults} from './utilities/anki.js';
25import {setupStubs} from './utilities/database.js';
26import {createFindKanjiOptions, createFindTermsOptions} from './utilities/translator.js';
27
28setupStubs();
29
30const dirname = path.dirname(fileURLToPath(import.meta.url));
31const dictionaryName = 'Test Dictionary 2';
32const test = await createTranslatorTest(void 0, path.join(dirname, 'data/dictionaries/valid-dictionary1'), dictionaryName);
33
34describe('Dictionary data', () => {
35 console.log('test');
36 const testInputsFilePath = path.join(dirname, 'data/translator-test-inputs.json');
37 /** @type {import('test/translator').TranslatorTestInputs} */
38 const {optionsPresets, tests} = parseJson(readFileSync(testInputsFilePath, {encoding: 'utf8'}));
39
40 const testResults1FilePath = path.join(dirname, 'data/translator-test-results.json');
41 const testResults2FilePath = path.join(dirname, 'data/translator-test-results-note-data1.json');
42 const testResults3FilePath = path.join(dirname, 'data/anki-note-builder-test-results.json');
43
44 /** @type {import('test/translator').TranslatorTestResults} */
45 const expectedResults1 = parseJson(readFileSync(testResults1FilePath, {encoding: 'utf8'}));
46 /** @type {import('test/translator').TranslatorTestNoteDataResults} */
47 const expectedResults2 = parseJson(readFileSync(testResults2FilePath, {encoding: 'utf8'}));
48 /** @type {import('test/translator').AnkiNoteBuilderTestResults} */
49 const expectedResults3 = parseJson(readFileSync(testResults3FilePath, {encoding: 'utf8'}));
50
51 const template = readFileSync(path.join(dirname, '../ext/data/templates/default-anki-field-templates.handlebars'), {encoding: 'utf8'});
52
53 const testCases = tests.map((data, i) => ({
54 data,
55 expected1: expectedResults1[i],
56 expected2: expectedResults2[i],
57 expected3: expectedResults3[i],
58 }));
59 describe.each(testCases)('Test %#: $data.name', ({data, expected1, expected2, expected3}) => {
60 test('Test', async ({window, translator, styles, expect}) => {
61 // The window property needs to be referenced for it to be initialized.
62 // It is needed for DOM access for structured content.
63 void window;
64 switch (data.func) {
65 case 'findTerms':
66 {
67 const {mode, text} = data;
68 const options = createFindTermsOptions(dictionaryName, optionsPresets, data.options);
69 const {dictionaryEntries, originalTextLength} = await translator.findTerms(mode, text, options);
70 const renderResults = mode !== 'simple' ? await getTemplateRenderResults(dictionaryEntries, mode, template, expect, styles) : null;
71 const noteDataList = mode !== 'simple' ? dictionaryEntries.map((dictionaryEntry) => createTestAnkiNoteData(dictionaryEntry, mode, styles)) : null;
72 expect.soft(originalTextLength).toStrictEqual(expected1.originalTextLength);
73 expect.soft(dictionaryEntries).toStrictEqual(expected1.dictionaryEntries);
74 expect.soft(noteDataList).toEqual(expected2.noteDataList);
75 expect.soft(renderResults).toStrictEqual(expected3.results);
76 }
77 break;
78 case 'findKanji':
79 {
80 const {text} = data;
81 const options = createFindKanjiOptions(dictionaryName, optionsPresets, data.options);
82 const dictionaryEntries = await translator.findKanji(text, options);
83 const renderResults = await getTemplateRenderResults(dictionaryEntries, 'split', template, expect);
84 const noteDataList = dictionaryEntries.map((dictionaryEntry) => createTestAnkiNoteData(dictionaryEntry, 'split'));
85 expect.soft(dictionaryEntries).toStrictEqual(expected1.dictionaryEntries);
86 expect.soft(noteDataList).toEqual(expected2.noteDataList);
87 expect.soft(renderResults).toStrictEqual(expected3.results);
88 }
89 break;
90 }
91 });
92 });
93});