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, writeFileSync} from 'fs';
19import {fileURLToPath} from 'node:url';
20import path from 'path';
21import {parseJson} from '../dev/json.js';
22import {createTranslatorTest} from './fixtures/translator-test.js';
23import {createTestAnkiNoteData, getTemplateRenderResults} from './utilities/anki.js';
24import {setupStubs} from './utilities/database.js';
25import {createFindKanjiOptions, createFindTermsOptions} from './utilities/translator.js';
26
27setupStubs();
28
29/**
30 * @param {string} fileName
31 * @param {unknown} content
32 */
33function writeJson(fileName, content) {
34 writeFileSync(fileName, JSON.stringify(content, null, 2) + '\n');
35}
36
37const dirname = path.dirname(fileURLToPath(import.meta.url));
38const dictionaryName = 'Test Dictionary 2';
39const test = await createTranslatorTest(void 0, path.join(dirname, 'data/dictionaries/valid-dictionary1'), dictionaryName);
40
41test('Write dictionary data expected data', async ({window, translator, styles, expect}) => {
42 // The window property needs to be referenced for it to be initialized.
43 // It is needed for DOM access for structured content.
44 void window;
45 const testInputsFilePath = path.join(dirname, 'data/translator-test-inputs.json');
46 /** @type {import('test/translator').TranslatorTestInputs} */
47 const {optionsPresets, tests} = parseJson(readFileSync(testInputsFilePath, {encoding: 'utf8'}));
48
49 const testResults1FilePath = path.join(dirname, 'data/translator-test-results.json');
50 const testResults2FilePath = path.join(dirname, 'data/translator-test-results-note-data1.json');
51 const testResults3FilePath = path.join(dirname, 'data/anki-note-builder-test-results.json');
52
53 /** @type {import('test/translator').TranslatorTestResults} */
54 const actualResults1 = [];
55 /** @type {import('test/translator').TranslatorTestNoteDataResults} */
56 const actualResults2 = [];
57 /** @type {import('test/translator').AnkiNoteBuilderTestResults} */
58 const actualResults3 = [];
59
60 const template = readFileSync(path.join(dirname, '../ext/data/templates/default-anki-field-templates.handlebars'), {encoding: 'utf8'});
61
62 for (const data of tests) {
63 const {name} = data;
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, null, styles) : null;
71 const noteDataList = mode !== 'simple' ? dictionaryEntries.map((dictionaryEntry) => createTestAnkiNoteData(dictionaryEntry, mode, styles)) : null;
72 actualResults1.push({name, originalTextLength, dictionaryEntries});
73 actualResults2.push({name, noteDataList});
74 actualResults3.push({name, results: renderResults});
75 }
76 break;
77 case 'findKanji':
78 {
79 const {text} = data;
80 const options = createFindKanjiOptions(dictionaryName, optionsPresets, data.options);
81 const dictionaryEntries = await translator.findKanji(text, options);
82 const renderResults = await getTemplateRenderResults(dictionaryEntries, 'split', template, null);
83 const noteDataList = dictionaryEntries.map((dictionaryEntry) => createTestAnkiNoteData(dictionaryEntry, 'split'));
84 actualResults1.push({name, dictionaryEntries});
85 actualResults2.push({name, noteDataList});
86 actualResults3.push({name, results: renderResults});
87 }
88 break;
89 }
90 }
91
92 expect(() => writeJson(testResults1FilePath, actualResults1)).not.toThrow();
93 expect(() => writeJson(testResults2FilePath, actualResults2)).not.toThrow();
94 expect(() => writeJson(testResults3FilePath, actualResults3)).not.toThrow();
95});