aboutsummaryrefslogtreecommitdiff
path: root/test/translator.test.js
diff options
context:
space:
mode:
Diffstat (limited to 'test/translator.test.js')
-rw-r--r--test/translator.test.js71
1 files changed, 29 insertions, 42 deletions
diff --git a/test/translator.test.js b/test/translator.test.js
index 3db560a7..59887d7e 100644
--- a/test/translator.test.js
+++ b/test/translator.test.js
@@ -16,50 +16,41 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
-import {IDBKeyRange, indexedDB} from 'fake-indexeddb';
-import fs from 'fs';
+import {readFileSync} from 'fs';
import {fileURLToPath} from 'node:url';
import path from 'path';
-import {expect, test, vi} from 'vitest';
-import {TranslatorVM} from '../dev/translator-vm';
-
-vi.stubGlobal('indexedDB', indexedDB);
-vi.stubGlobal('IDBKeyRange', IDBKeyRange);
+import {describe} from 'vitest';
+import {createTranslatorTest} from './fixtures/translator-test.js';
+import {createTestAnkiNoteData} from './utilities/anki.js';
+import {createFindOptions} from './utilities/translator.js';
const dirname = path.dirname(fileURLToPath(import.meta.url));
-/** */
-async function main() {
- const translatorVM = new TranslatorVM();
- const dictionaryDirectory = path.join(dirname, 'data', 'dictionaries', 'valid-dictionary1');
- await translatorVM.prepare(dictionaryDirectory, 'Test Dictionary 2');
+const testInputsFilePath = path.join(dirname, 'data/translator-test-inputs.json');
+/** @type {import('test/anki-note-builder').TranslatorTestInputs} */
+const {optionsPresets, tests} = JSON.parse(readFileSync(testInputsFilePath, {encoding: 'utf8'}));
- const testInputsFilePath = path.join(dirname, 'data', 'translator-test-inputs.json');
- const {optionsPresets, tests} = JSON.parse(fs.readFileSync(testInputsFilePath, {encoding: 'utf8'}));
+const testResults1FilePath = path.join(dirname, 'data/translator-test-results.json');
+const expectedResults1 = JSON.parse(readFileSync(testResults1FilePath, {encoding: 'utf8'}));
- const testResults1FilePath = path.join(dirname, 'data', 'translator-test-results.json');
- const expectedResults1 = JSON.parse(fs.readFileSync(testResults1FilePath, {encoding: 'utf8'}));
- const actualResults1 = [];
+const testResults2FilePath = path.join(dirname, 'data/translator-test-results-note-data1.json');
+const expectedResults2 = JSON.parse(readFileSync(testResults2FilePath, {encoding: 'utf8'}));
- const testResults2FilePath = path.join(dirname, 'data', 'translator-test-results-note-data1.json');
- const expectedResults2 = JSON.parse(fs.readFileSync(testResults2FilePath, {encoding: 'utf8'}));
- const actualResults2 = [];
+const dictionaryName = 'Test Dictionary 2';
+const test = await createTranslatorTest(void 0, path.join(dirname, 'data/dictionaries/valid-dictionary1'), dictionaryName);
- for (let i = 0, ii = tests.length; i < ii; ++i) {
- test(`${i}`, async () => {
- const t = tests[i];
- const expected1 = expectedResults1[i];
- const expected2 = expectedResults2[i];
- switch (t.func) {
+describe('Translator', () => {
+ const testData = tests.map((data, i) => ({data, expected1: expectedResults1[i], expected2: expectedResults2[i]}));
+ describe.each(testData)('Test %#: $data.name', ({data, expected1, expected2}) => {
+ test('Test', async ({translator, ankiNoteDataCreator, expect}) => {
+ switch (data.func) {
case 'findTerms':
{
- const {name, mode, text} = t;
+ const {mode, text} = data;
/** @type {import('translation').FindTermsOptions} */
- const options = translatorVM.buildOptions(optionsPresets, t.options);
- const {dictionaryEntries, originalTextLength} = structuredClone(await translatorVM.translator.findTerms(mode, text, options));
- const noteDataList = mode !== 'simple' ? structuredClone(dictionaryEntries.map((dictionaryEntry) => translatorVM.createTestAnkiNoteData(structuredClone(dictionaryEntry), mode))) : null;
- actualResults1.push({name, originalTextLength, dictionaryEntries});
- actualResults2.push({name, noteDataList});
+ const options = createFindOptions(dictionaryName, optionsPresets, data.options);
+ const {dictionaryEntries, originalTextLength} = await translator.findTerms(mode, text, options);
+ const noteDataList = mode !== 'simple' ? dictionaryEntries.map((dictionaryEntry) => createTestAnkiNoteData(ankiNoteDataCreator, dictionaryEntry, mode)) : null;
expect(originalTextLength).toStrictEqual(expected1.originalTextLength);
expect(dictionaryEntries).toStrictEqual(expected1.dictionaryEntries);
expect(noteDataList).toEqual(expected2.noteDataList);
@@ -67,20 +58,16 @@ async function main() {
break;
case 'findKanji':
{
- const {name, text} = t;
+ const {text} = data;
/** @type {import('translation').FindKanjiOptions} */
- const options = translatorVM.buildOptions(optionsPresets, t.options);
- const dictionaryEntries = structuredClone(await translatorVM.translator.findKanji(text, options));
- const noteDataList = structuredClone(dictionaryEntries.map((dictionaryEntry) => translatorVM.createTestAnkiNoteData(structuredClone(dictionaryEntry), 'split')));
- actualResults1.push({name, dictionaryEntries});
- actualResults2.push({name, noteDataList});
+ const options = createFindOptions(dictionaryName, optionsPresets, data.options);
+ const dictionaryEntries = await translator.findKanji(text, options);
+ const noteDataList = dictionaryEntries.map((dictionaryEntry) => createTestAnkiNoteData(ankiNoteDataCreator, dictionaryEntry, 'split'));
expect(dictionaryEntries).toStrictEqual(expected1.dictionaryEntries);
expect(noteDataList).toEqual(expected2.noteDataList);
}
break;
}
});
- }
-}
-
-await main();
+ });
+});