aboutsummaryrefslogtreecommitdiff
path: root/test/translator.test.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2023-12-18 22:47:29 -0500
committerGitHub <noreply@github.com>2023-12-19 03:47:29 +0000
commit46821eeb7fc9e00645aeae1c7fce3e6e7b637ca0 (patch)
tree7ed0b027946081bbc88bffefc0bfcb2c20d15106 /test/translator.test.js
parent521e87d01142063c785054741d3703de37a1636c (diff)
Test fixtures (#371)
* Move and rename document-test.js file * Only load HTML file content once * Move testDoc construction * Add createTranslatorTest * Add utilities * Update translator tests * Rename * Refactor anki note builder tests * Refactor * Use internal expect * Updates * Remove actual results * Remove concurrent
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();
+ });
+});