aboutsummaryrefslogtreecommitdiff
path: root/test/anki-note-builder.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/anki-note-builder.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/anki-note-builder.test.js')
-rw-r--r--test/anki-note-builder.test.js130
1 files changed, 44 insertions, 86 deletions
diff --git a/test/anki-note-builder.test.js b/test/anki-note-builder.test.js
index 42ee2290..cc136957 100644
--- a/test/anki-note-builder.test.js
+++ b/test/anki-note-builder.test.js
@@ -18,56 +18,20 @@
// @vitest-environment jsdom
-import 'fake-indexeddb/auto';
-import fs from 'fs';
+import {readFileSync} from 'fs';
import {fileURLToPath} from 'node:url';
import path from 'path';
-import url from 'url';
-import {describe, test, vi} from 'vitest';
-import {TranslatorVM} from '../dev/translator-vm.js';
+import {describe, vi} from 'vitest';
import {AnkiNoteBuilder} from '../ext/js/data/anki-note-builder.js';
import {JapaneseUtil} from '../ext/js/language/sandbox/japanese-util.js';
+import {createTranslatorTest} from './fixtures/translator-test.js';
+import {createFindOptions} from './utilities/translator.js';
const dirname = path.dirname(fileURLToPath(import.meta.url));
-/**
- * @param {string} url2
- * @returns {Promise<import('dev/vm').PseudoFetchResponse>}
- */
-async function fetch(url2) {
- const extDir = path.join(dirname, '..', 'ext');
- let filePath;
- try {
- filePath = url.fileURLToPath(url2);
- } catch (e) {
- filePath = path.resolve(extDir, url2.replace(/^[/\\]/, ''));
- }
- await Promise.resolve();
- const content = fs.readFileSync(filePath, {encoding: null});
- return {
- ok: true,
- status: 200,
- statusText: 'OK',
- text: async () => Promise.resolve(content.toString('utf8')),
- json: async () => Promise.resolve(JSON.parse(content.toString('utf8')))
- };
-}
-vi.stubGlobal('fetch', fetch);
vi.mock('../ext/js/templates/template-renderer-proxy.js', async () => await import('../test/mocks/template-renderer-proxy.js'));
/**
- * @returns {Promise<TranslatorVM>}
- */
-async function createVM() {
- const dictionaryDirectory = path.join(dirname, 'data', 'dictionaries', 'valid-dictionary1');
- const vm = new TranslatorVM();
-
- await vm.prepare(dictionaryDirectory, 'Test Dictionary 2');
-
- return vm;
-}
-
-/**
* @param {'terms'|'kanji'} type
* @returns {string[]}
*/
@@ -205,50 +169,44 @@ async function getRenderResults(dictionaryEntries, type, mode, template, expect)
}
-/** */
-async function main() {
- const vm = await createVM();
-
- 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', 'anki-note-builder-test-results.json');
- const expectedResults1 = JSON.parse(fs.readFileSync(testResults1FilePath, {encoding: 'utf8'}));
- const actualResults1 = [];
-
- const template = fs.readFileSync(path.join(dirname, '..', 'ext', 'data/templates/default-anki-field-templates.handlebars'), {encoding: 'utf8'});
-
- describe.concurrent('AnkiNoteBuilder', () => {
- for (let i = 0, ii = tests.length; i < ii; ++i) {
- const t = tests[i];
- test(`${t.name}`, async ({expect}) => {
- const expected1 = expectedResults1[i];
- switch (t.func) {
- case 'findTerms':
- {
- const {name, mode, text} = t;
- /** @type {import('translation').FindTermsOptions} */
- const options = vm.buildOptions(optionsPresets, t.options);
- const {dictionaryEntries} = structuredClone(await vm.translator.findTerms(mode, text, options));
- const results = mode !== 'simple' ? structuredClone(await getRenderResults(dictionaryEntries, 'terms', mode, template, expect)) : null;
- actualResults1.push({name, results});
- expect(results).toStrictEqual(expected1.results);
- }
- break;
- case 'findKanji':
- {
- const {name, text} = t;
- /** @type {import('translation').FindKanjiOptions} */
- const options = vm.buildOptions(optionsPresets, t.options);
- const dictionaryEntries = structuredClone(await vm.translator.findKanji(text, options));
- const results = structuredClone(await getRenderResults(dictionaryEntries, 'kanji', 'split', template, expect));
- actualResults1.push({name, results});
- expect(results).toStrictEqual(expected1.results);
- }
- break;
- }
- });
- }
+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 testResults1FilePath = path.join(dirname, 'data/anki-note-builder-test-results.json');
+const expectedResults1 = JSON.parse(readFileSync(testResults1FilePath, {encoding: 'utf8'}));
+
+const template = readFileSync(path.join(dirname, '../ext/data/templates/default-anki-field-templates.handlebars'), {encoding: 'utf8'});
+
+const dictionaryName = 'Test Dictionary 2';
+const test = await createTranslatorTest(void 0, path.join(dirname, 'data/dictionaries/valid-dictionary1'), dictionaryName);
+
+describe('AnkiNoteBuilder', () => {
+ const testData = tests.map((data, i) => ({data, expected1: expectedResults1[i]}));
+ describe.each(testData)('Test %#: $data.name', ({data, expected1}) => {
+ test('Test', async ({expect, translator}) => {
+ switch (data.func) {
+ case 'findTerms':
+ {
+ const {mode, text} = data;
+ /** @type {import('translation').FindTermsOptions} */
+ const options = createFindOptions(dictionaryName, optionsPresets, data.options);
+ const {dictionaryEntries} = await translator.findTerms(mode, text, options);
+ const results = mode !== 'simple' ? await getRenderResults(dictionaryEntries, 'terms', mode, template, expect) : null;
+ expect(results).toStrictEqual(expected1.results);
+ }
+ break;
+ case 'findKanji':
+ {
+ const {text} = data;
+ /** @type {import('translation').FindKanjiOptions} */
+ const options = createFindOptions(dictionaryName, optionsPresets, data.options);
+ const dictionaryEntries = await translator.findKanji(text, options);
+ const results = await getRenderResults(dictionaryEntries, 'kanji', 'split', template, expect);
+ expect(results).toStrictEqual(expected1.results);
+ }
+ break;
+ }
+ });
});
-}
-await main();
+});