From 11d2b933be3f775fe1723a4a60452635b0aa6cfd Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Fri, 22 Dec 2023 07:52:33 -0500 Subject: Dictionary data tests + write mode (#415) * Rename test * Refactor * Create new dictionary-data.test.js * Move utility functions * Remove old tests * Slight refactor * Add command to rebuild test data * Clarify name * Don't expect in write mode * Ignore config file --- test/anki-note-builder.test.js | 215 ---------------------------------- test/data/json.json | 1 + test/data/vitest.write.config.json | 7 ++ test/dictionary-data-validate.test.js | 58 +++++++++ test/dictionary-data.test.js | 88 ++++++++++++++ test/dictionary-data.write.js | 91 ++++++++++++++ test/dictionary.test.js | 65 ---------- test/fixtures/translator-test.js | 8 +- test/translator.test.js | 76 ------------ test/utilities/anki.js | 145 +++++++++++++++++++++++ 10 files changed, 396 insertions(+), 358 deletions(-) delete mode 100644 test/anki-note-builder.test.js create mode 100644 test/data/vitest.write.config.json create mode 100644 test/dictionary-data-validate.test.js create mode 100644 test/dictionary-data.test.js create mode 100644 test/dictionary-data.write.js delete mode 100644 test/dictionary.test.js delete mode 100644 test/translator.test.js (limited to 'test') diff --git a/test/anki-note-builder.test.js b/test/anki-note-builder.test.js deleted file mode 100644 index 35db5107..00000000 --- a/test/anki-note-builder.test.js +++ /dev/null @@ -1,215 +0,0 @@ -/* - * Copyright (C) 2023 Yomitan Authors - * Copyright (C) 2021-2022 Yomichan Authors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -// @vitest-environment jsdom - -import {readFileSync} from 'fs'; -import {fileURLToPath} from 'node:url'; -import path from 'path'; -import {describe} from 'vitest'; -import {parseJson} from '../dev/json.js'; -import {AnkiNoteBuilder} from '../ext/js/data/anki-note-builder.js'; -import {JapaneseUtil} from '../ext/js/language/sandbox/japanese-util.js'; -import {AnkiTemplateRenderer} from '../ext/js/templates/sandbox/anki-template-renderer.js'; -import {createTranslatorTest} from './fixtures/translator-test.js'; -import {createFindOptions} from './utilities/translator.js'; - -const dirname = path.dirname(fileURLToPath(import.meta.url)); - -/** - * @param {'terms'|'kanji'} type - * @returns {string[]} - */ -function getFieldMarkers(type) { - switch (type) { - case 'terms': - return [ - 'audio', - 'clipboard-image', - 'clipboard-text', - 'cloze-body', - 'cloze-prefix', - 'cloze-suffix', - 'conjugation', - 'dictionary', - 'document-title', - 'expression', - 'frequencies', - 'furigana', - 'furigana-plain', - 'glossary', - 'glossary-brief', - 'glossary-no-dictionary', - 'part-of-speech', - 'pitch-accents', - 'pitch-accent-graphs', - 'pitch-accent-positions', - 'reading', - 'screenshot', - 'search-query', - 'selection-text', - 'sentence', - 'sentence-furigana', - 'tags', - 'url' - ]; - case 'kanji': - return [ - 'character', - 'clipboard-image', - 'clipboard-text', - 'cloze-body', - 'cloze-prefix', - 'cloze-suffix', - 'dictionary', - 'document-title', - 'glossary', - 'kunyomi', - 'onyomi', - 'screenshot', - 'search-query', - 'selection-text', - 'sentence', - 'sentence-furigana', - 'stroke-count', - 'tags', - 'url' - ]; - default: - return []; - } -} - -/** - * @param {import('dictionary').DictionaryEntry[]} dictionaryEntries - * @param {'terms'|'kanji'} type - * @param {import('settings').ResultOutputMode} mode - * @param {string} template - * @param {import('vitest').ExpectStatic} expect - * @returns {Promise} - */ -async function getRenderResults(dictionaryEntries, type, mode, template, expect) { - const markers = getFieldMarkers(type); - /** @type {import('anki-note-builder').Field[]} */ - const fields = []; - for (const marker of markers) { - fields.push([marker, `{${marker}}`]); - } - - const ankiTemplateRenderer = new AnkiTemplateRenderer(); - await ankiTemplateRenderer.prepare(); - const japaneseUtil = new JapaneseUtil(null); - const clozePrefix = 'cloze-prefix'; - const clozeSuffix = 'cloze-suffix'; - const results = []; - for (const dictionaryEntry of dictionaryEntries) { - let source = ''; - switch (dictionaryEntry.type) { - case 'kanji': - source = dictionaryEntry.character; - break; - case 'term': - if (dictionaryEntry.headwords.length > 0 && dictionaryEntry.headwords[0].sources.length > 0) { - source = dictionaryEntry.headwords[0].sources[0].originalText; - } - break; - } - const ankiNoteBuilder = new AnkiNoteBuilder(japaneseUtil, ankiTemplateRenderer.templateRenderer); - const context = { - url: 'url:', - sentence: { - text: `${clozePrefix}${source}${clozeSuffix}`, - offset: clozePrefix.length - }, - documentTitle: 'title', - query: 'query', - fullQuery: 'fullQuery' - }; - /** @type {import('anki-note-builder').CreateNoteDetails} */ - const details = { - dictionaryEntry, - mode: 'test', - context, - template, - deckName: 'deckName', - modelName: 'modelName', - fields, - tags: ['yomitan'], - checkForDuplicates: true, - duplicateScope: 'collection', - duplicateScopeCheckAllModels: false, - resultOutputMode: mode, - glossaryLayoutMode: 'default', - compactTags: false, - requirements: [], - mediaOptions: null - }; - const {note: {fields: noteFields}, errors} = await ankiNoteBuilder.createNote(details); - for (const error of errors) { - console.error(error); - } - expect(errors.length).toStrictEqual(0); - results.push(noteFields); - } - - return results; -} - - -const testInputsFilePath = path.join(dirname, 'data/translator-test-inputs.json'); -/** @type {import('test/translator').TranslatorTestInputs} */ -const {optionsPresets, tests} = parseJson(readFileSync(testInputsFilePath, {encoding: 'utf8'})); - -const testResults1FilePath = path.join(dirname, 'data/anki-note-builder-test-results.json'); -/** @type {import('test/translator').AnkiNoteBuilderTestResults} */ -const expectedResults1 = parseJson(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; - } - }); - }); -}); diff --git a/test/data/json.json b/test/data/json.json index 6d806263..1f856033 100644 --- a/test/data/json.json +++ b/test/data/json.json @@ -22,6 +22,7 @@ {"path": "test/data/dictionaries/invalid-dictionary6/term_meta_bank_1.json", "ignore": true}, {"path": "test/data/dictionaries/invalid-dictionary6/index.json", "ignore": true}, {"path": "test/jsconfig.json", "ignore": true}, + {"path": "test/data/vitest.write.config.json", "ignore": true}, { "path": "dev/data/manifest-variants.json", diff --git a/test/data/vitest.write.config.json b/test/data/vitest.write.config.json new file mode 100644 index 00000000..ecb4bd84 --- /dev/null +++ b/test/data/vitest.write.config.json @@ -0,0 +1,7 @@ +{ + "test": { + "include": [ + "../**/*.write.js" + ] + } +} \ No newline at end of file diff --git a/test/dictionary-data-validate.test.js b/test/dictionary-data-validate.test.js new file mode 100644 index 00000000..c54f7dbb --- /dev/null +++ b/test/dictionary-data-validate.test.js @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2023 Yomitan Authors + * Copyright (C) 2020-2022 Yomichan Authors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +import {fileURLToPath} from 'node:url'; +import path from 'path'; +import {describe, test} from 'vitest'; +import * as dictionaryValidate from '../dev/dictionary-validate.js'; +import {createDictionaryArchive} from '../dev/util.js'; + +const dirname = path.dirname(fileURLToPath(import.meta.url)); + +/** + * @param {string} dictionary + * @param {string} [dictionaryName] + * @returns {import('jszip')} + */ +function createTestDictionaryArchive(dictionary, dictionaryName) { + const dictionaryDirectory = path.join(dirname, 'data', 'dictionaries', dictionary); + return createDictionaryArchive(dictionaryDirectory, dictionaryName); +} + +describe('Dictionary validation', () => { + const testCases = [ + {name: 'valid-dictionary1', valid: true}, + {name: 'invalid-dictionary1', valid: false}, + {name: 'invalid-dictionary2', valid: false}, + {name: 'invalid-dictionary3', valid: false}, + {name: 'invalid-dictionary4', valid: false}, + {name: 'invalid-dictionary5', valid: false}, + {name: 'invalid-dictionary6', valid: false} + ]; + const schemas = dictionaryValidate.getSchemas(); + describe.each(testCases)('Test dictionary $name', ({name, valid}) => { + test(`Should be ${valid ? 'valid' : 'invalid'}`, async ({expect}) => { + const archive = createTestDictionaryArchive(name); + if (valid) { + await expect(dictionaryValidate.validateDictionary(null, archive, schemas)).resolves.not.toThrow(); + } else { + await expect(dictionaryValidate.validateDictionary(null, archive, schemas)).rejects.toThrow(); + } + }); + }); +}); diff --git a/test/dictionary-data.test.js b/test/dictionary-data.test.js new file mode 100644 index 00000000..b3a9c57d --- /dev/null +++ b/test/dictionary-data.test.js @@ -0,0 +1,88 @@ +/* + * Copyright (C) 2023 Yomitan Authors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +import {readFileSync} from 'fs'; +import {fileURLToPath} from 'node:url'; +import path from 'path'; +import {describe} from 'vitest'; +import {parseJson} from '../dev/json.js'; +import {createTranslatorTest} from './fixtures/translator-test.js'; +import {createTestAnkiNoteData, getTemplateRenderResults} from './utilities/anki.js'; +import {createFindOptions} from './utilities/translator.js'; + +const dirname = path.dirname(fileURLToPath(import.meta.url)); +const dictionaryName = 'Test Dictionary 2'; +const test = await createTranslatorTest(void 0, path.join(dirname, 'data/dictionaries/valid-dictionary1'), dictionaryName); + +describe('Dictionary data', () => { + const testInputsFilePath = path.join(dirname, 'data/translator-test-inputs.json'); + /** @type {import('test/translator').TranslatorTestInputs} */ + const {optionsPresets, tests} = parseJson(readFileSync(testInputsFilePath, {encoding: 'utf8'})); + + const testResults1FilePath = path.join(dirname, 'data/translator-test-results.json'); + const testResults2FilePath = path.join(dirname, 'data/translator-test-results-note-data1.json'); + const testResults3FilePath = path.join(dirname, 'data/anki-note-builder-test-results.json'); + + /** @type {import('test/translator').TranslatorTestResults} */ + const expectedResults1 = parseJson(readFileSync(testResults1FilePath, {encoding: 'utf8'})); + /** @type {import('test/translator').TranslatorTestNoteDataResults} */ + const expectedResults2 = parseJson(readFileSync(testResults2FilePath, {encoding: 'utf8'})); + /** @type {import('test/translator').AnkiNoteBuilderTestResults} */ + const expectedResults3 = parseJson(readFileSync(testResults3FilePath, {encoding: 'utf8'})); + + const template = readFileSync(path.join(dirname, '../ext/data/templates/default-anki-field-templates.handlebars'), {encoding: 'utf8'}); + + const testCases = tests.map((data, i) => ({ + data, + expected1: expectedResults1[i], + expected2: expectedResults2[i], + expected3: expectedResults3[i] + })); + describe.each(testCases)('Test %#: $data.name', ({data, expected1, expected2, expected3}) => { + test('Test', async ({translator, ankiNoteDataCreator, expect}) => { + switch (data.func) { + case 'findTerms': + { + const {mode, text} = data; + /** @type {import('translation').FindTermsOptions} */ + const options = createFindOptions(dictionaryName, optionsPresets, data.options); + const {dictionaryEntries, originalTextLength} = await translator.findTerms(mode, text, options); + const renderResults = mode !== 'simple' ? await getTemplateRenderResults(dictionaryEntries, 'terms', mode, template, expect) : null; + const noteDataList = mode !== 'simple' ? dictionaryEntries.map((dictionaryEntry) => createTestAnkiNoteData(ankiNoteDataCreator, dictionaryEntry, mode)) : null; + expect.soft(originalTextLength).toStrictEqual(expected1.originalTextLength); + expect.soft(dictionaryEntries).toStrictEqual(expected1.dictionaryEntries); + expect.soft(noteDataList).toEqual(expected2.noteDataList); + expect.soft(renderResults).toStrictEqual(expected3.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 renderResults = await getTemplateRenderResults(dictionaryEntries, 'kanji', 'split', template, expect); + const noteDataList = dictionaryEntries.map((dictionaryEntry) => createTestAnkiNoteData(ankiNoteDataCreator, dictionaryEntry, 'split')); + expect.soft(dictionaryEntries).toStrictEqual(expected1.dictionaryEntries); + expect.soft(noteDataList).toEqual(expected2.noteDataList); + expect.soft(renderResults).toStrictEqual(expected3.results); + } + break; + } + }); + }); +}); diff --git a/test/dictionary-data.write.js b/test/dictionary-data.write.js new file mode 100644 index 00000000..0f6bbfcb --- /dev/null +++ b/test/dictionary-data.write.js @@ -0,0 +1,91 @@ +/* + * Copyright (C) 2023 Yomitan Authors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +import {readFileSync, writeFileSync} from 'fs'; +import {fileURLToPath} from 'node:url'; +import path from 'path'; +import {parseJson} from '../dev/json.js'; +import {createTranslatorTest} from './fixtures/translator-test.js'; +import {createTestAnkiNoteData, getTemplateRenderResults} from './utilities/anki.js'; +import {createFindOptions} from './utilities/translator.js'; + +/** + * @param {string} fileName + * @param {unknown} content + */ +function writeJson(fileName, content) { + writeFileSync(fileName, JSON.stringify(content, null, 2)); +} + +const dirname = path.dirname(fileURLToPath(import.meta.url)); +const dictionaryName = 'Test Dictionary 2'; +const test = await createTranslatorTest(void 0, path.join(dirname, 'data/dictionaries/valid-dictionary1'), dictionaryName); + +test('Write dictionary data expected data', async ({translator, ankiNoteDataCreator, expect}) => { + const testInputsFilePath = path.join(dirname, 'data/translator-test-inputs.json'); + /** @type {import('test/translator').TranslatorTestInputs} */ + const {optionsPresets, tests} = parseJson(readFileSync(testInputsFilePath, {encoding: 'utf8'})); + + const testResults1FilePath = path.join(dirname, 'data/translator-test-results.json'); + const testResults2FilePath = path.join(dirname, 'data/translator-test-results-note-data1.json'); + const testResults3FilePath = path.join(dirname, 'data/anki-note-builder-test-results.json'); + + /** @type {import('test/translator').TranslatorTestResults} */ + const actualResults1 = []; + /** @type {import('test/translator').TranslatorTestNoteDataResults} */ + const actualResults2 = []; + /** @type {import('test/translator').AnkiNoteBuilderTestResults} */ + const actualResults3 = []; + + const template = readFileSync(path.join(dirname, '../ext/data/templates/default-anki-field-templates.handlebars'), {encoding: 'utf8'}); + + for (const data of tests) { + const {name} = data; + switch (data.func) { + case 'findTerms': + { + const {mode, text} = data; + /** @type {import('translation').FindTermsOptions} */ + const options = createFindOptions(dictionaryName, optionsPresets, data.options); + const {dictionaryEntries, originalTextLength} = await translator.findTerms(mode, text, options); + const renderResults = mode !== 'simple' ? await getTemplateRenderResults(dictionaryEntries, 'terms', mode, template, null) : null; + const noteDataList = mode !== 'simple' ? dictionaryEntries.map((dictionaryEntry) => createTestAnkiNoteData(ankiNoteDataCreator, dictionaryEntry, mode)) : null; + actualResults1.push({name, originalTextLength, dictionaryEntries}); + actualResults2.push({name, noteDataList}); + actualResults3.push({name, results: renderResults}); + } + 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 renderResults = await getTemplateRenderResults(dictionaryEntries, 'kanji', 'split', template, null); + const noteDataList = dictionaryEntries.map((dictionaryEntry) => createTestAnkiNoteData(ankiNoteDataCreator, dictionaryEntry, 'split')); + actualResults1.push({name, dictionaryEntries}); + actualResults2.push({name, noteDataList}); + actualResults3.push({name, results: renderResults}); + } + break; + } + } + + expect(() => writeJson(testResults1FilePath, actualResults1)).not.toThrow(); + expect(() => writeJson(testResults2FilePath, actualResults2)).not.toThrow(); + expect(() => writeJson(testResults3FilePath, actualResults3)).not.toThrow(); +}); diff --git a/test/dictionary.test.js b/test/dictionary.test.js deleted file mode 100644 index e516bd8e..00000000 --- a/test/dictionary.test.js +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright (C) 2023 Yomitan Authors - * Copyright (C) 2020-2022 Yomichan Authors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -import {fileURLToPath} from 'node:url'; -import path from 'path'; -import {expect, test} from 'vitest'; -import * as dictionaryValidate from '../dev/dictionary-validate.js'; -import {createDictionaryArchive} from '../dev/util.js'; - -const dirname = path.dirname(fileURLToPath(import.meta.url)); - -/** - * @param {string} dictionary - * @param {string} [dictionaryName] - * @returns {import('jszip')} - */ -function createTestDictionaryArchive(dictionary, dictionaryName) { - const dictionaryDirectory = path.join(dirname, 'data', 'dictionaries', dictionary); - return createDictionaryArchive(dictionaryDirectory, dictionaryName); -} - - -/** */ -async function main() { - const dictionaries = [ - {name: 'valid-dictionary1', valid: true}, - {name: 'invalid-dictionary1', valid: false}, - {name: 'invalid-dictionary2', valid: false}, - {name: 'invalid-dictionary3', valid: false}, - {name: 'invalid-dictionary4', valid: false}, - {name: 'invalid-dictionary5', valid: false}, - {name: 'invalid-dictionary6', valid: false} - ]; - - const schemas = dictionaryValidate.getSchemas(); - - for (const {name, valid} of dictionaries) { - test(`${name} is ${valid ? 'valid' : 'invalid'}`, async () => { - const archive = createTestDictionaryArchive(name); - - if (valid) { - await expect(dictionaryValidate.validateDictionary(null, archive, schemas)).resolves.not.toThrow(); - } else { - await expect(dictionaryValidate.validateDictionary(null, archive, schemas)).rejects.toThrow(); - } - }); - } -} - -await main(); diff --git a/test/fixtures/translator-test.js b/test/fixtures/translator-test.js index 3304c587..0afbe1f0 100644 --- a/test/fixtures/translator-test.js +++ b/test/fixtures/translator-test.js @@ -90,8 +90,12 @@ export async function createTranslatorTest(htmlFilePath, dictionaryDirectory, di window: async ({window}, use) => { await use(window); }, // eslint-disable-next-line no-empty-pattern translator: async ({}, use) => { await use(translator); }, - // eslint-disable-next-line no-empty-pattern - ankiNoteDataCreator: async ({}, use) => { await use(ankiNoteDataCreator); } + ankiNoteDataCreator: async ({window}, use) => { + // The window property needs to be referenced for it to be initialized. + // It is needed for DOM access for structured content. + void window; + await use(ankiNoteDataCreator); + } }); return result; } diff --git a/test/translator.test.js b/test/translator.test.js deleted file mode 100644 index 42a9076e..00000000 --- a/test/translator.test.js +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright (C) 2023 Yomitan Authors - * Copyright (C) 2020-2022 Yomichan Authors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -import {readFileSync} from 'fs'; -import {fileURLToPath} from 'node:url'; -import path from 'path'; -import {describe} from 'vitest'; -import {parseJson} from '../dev/json.js'; -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)); - -const testInputsFilePath = path.join(dirname, 'data/translator-test-inputs.json'); -/** @type {import('test/translator').TranslatorTestInputs} */ -const {optionsPresets, tests} = parseJson(readFileSync(testInputsFilePath, {encoding: 'utf8'})); - -const testResults1FilePath = path.join(dirname, 'data/translator-test-results.json'); -/** @type {import('test/translator').TranslatorTestResults} */ -const expectedResults1 = parseJson(readFileSync(testResults1FilePath, {encoding: 'utf8'})); - -const testResults2FilePath = path.join(dirname, 'data/translator-test-results-note-data1.json'); -/** @type {import('test/translator').TranslatorTestNoteDataResults} */ -const expectedResults2 = parseJson(readFileSync(testResults2FilePath, {encoding: 'utf8'})); - -const dictionaryName = 'Test Dictionary 2'; -const test = await createTranslatorTest(void 0, path.join(dirname, 'data/dictionaries/valid-dictionary1'), dictionaryName); - -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 {mode, text} = data; - /** @type {import('translation').FindTermsOptions} */ - 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); - } - 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 noteDataList = dictionaryEntries.map((dictionaryEntry) => createTestAnkiNoteData(ankiNoteDataCreator, dictionaryEntry, 'split')); - expect(dictionaryEntries).toStrictEqual(expected1.dictionaryEntries); - expect(noteDataList).toEqual(expected2.noteDataList); - } - break; - } - }); - }); -}); diff --git a/test/utilities/anki.js b/test/utilities/anki.js index 0a651c30..4b73f6b9 100644 --- a/test/utilities/anki.js +++ b/test/utilities/anki.js @@ -15,6 +15,10 @@ * along with this program. If not, see . */ +import {AnkiNoteBuilder} from '../../ext/js/data/anki-note-builder.js'; +import {JapaneseUtil} from '../../ext/js/language/sandbox/japanese-util.js'; +import {AnkiTemplateRenderer} from '../../ext/js/templates/sandbox/anki-template-renderer.js'; + /** * @param {import('../../ext/js/data/sandbox/anki-note-data-creator.js').AnkiNoteDataCreator} ankiNoteDataCreator * @param {import('dictionary').DictionaryEntry} dictionaryEntry @@ -42,3 +46,144 @@ export function createTestAnkiNoteData(ankiNoteDataCreator, dictionaryEntry, mod }; return ankiNoteDataCreator.create(marker, data); } + +/** + * @param {'terms'|'kanji'} type + * @returns {string[]} + */ +function getFieldMarkers(type) { + switch (type) { + case 'terms': + return [ + 'audio', + 'clipboard-image', + 'clipboard-text', + 'cloze-body', + 'cloze-prefix', + 'cloze-suffix', + 'conjugation', + 'dictionary', + 'document-title', + 'expression', + 'frequencies', + 'furigana', + 'furigana-plain', + 'glossary', + 'glossary-brief', + 'glossary-no-dictionary', + 'part-of-speech', + 'pitch-accents', + 'pitch-accent-graphs', + 'pitch-accent-positions', + 'reading', + 'screenshot', + 'search-query', + 'selection-text', + 'sentence', + 'sentence-furigana', + 'tags', + 'url' + ]; + case 'kanji': + return [ + 'character', + 'clipboard-image', + 'clipboard-text', + 'cloze-body', + 'cloze-prefix', + 'cloze-suffix', + 'dictionary', + 'document-title', + 'glossary', + 'kunyomi', + 'onyomi', + 'screenshot', + 'search-query', + 'selection-text', + 'sentence', + 'sentence-furigana', + 'stroke-count', + 'tags', + 'url' + ]; + default: + return []; + } +} + +/** + * @param {import('dictionary').DictionaryEntry[]} dictionaryEntries + * @param {'terms'|'kanji'} type + * @param {import('settings').ResultOutputMode} mode + * @param {string} template + * @param {?import('vitest').ExpectStatic} expect + * @returns {Promise} + */ +export async function getTemplateRenderResults(dictionaryEntries, type, mode, template, expect) { + const markers = getFieldMarkers(type); + /** @type {import('anki-note-builder').Field[]} */ + const fields = []; + for (const marker of markers) { + fields.push([marker, `{${marker}}`]); + } + + const ankiTemplateRenderer = new AnkiTemplateRenderer(); + await ankiTemplateRenderer.prepare(); + const japaneseUtil = new JapaneseUtil(null); + const clozePrefix = 'cloze-prefix'; + const clozeSuffix = 'cloze-suffix'; + const results = []; + for (const dictionaryEntry of dictionaryEntries) { + let source = ''; + switch (dictionaryEntry.type) { + case 'kanji': + source = dictionaryEntry.character; + break; + case 'term': + if (dictionaryEntry.headwords.length > 0 && dictionaryEntry.headwords[0].sources.length > 0) { + source = dictionaryEntry.headwords[0].sources[0].originalText; + } + break; + } + const ankiNoteBuilder = new AnkiNoteBuilder(japaneseUtil, ankiTemplateRenderer.templateRenderer); + const context = { + url: 'url:', + sentence: { + text: `${clozePrefix}${source}${clozeSuffix}`, + offset: clozePrefix.length + }, + documentTitle: 'title', + query: 'query', + fullQuery: 'fullQuery' + }; + /** @type {import('anki-note-builder').CreateNoteDetails} */ + const details = { + dictionaryEntry, + mode: 'test', + context, + template, + deckName: 'deckName', + modelName: 'modelName', + fields, + tags: ['yomitan'], + checkForDuplicates: true, + duplicateScope: 'collection', + duplicateScopeCheckAllModels: false, + resultOutputMode: mode, + glossaryLayoutMode: 'default', + compactTags: false, + requirements: [], + mediaOptions: null + }; + const {note: {fields: noteFields}, errors} = await ankiNoteBuilder.createNote(details); + for (const error of errors) { + console.error(error); + } + if (expect !== null) { + expect(errors.length).toStrictEqual(0); + } + results.push(noteFields); + } + + return results; +} -- cgit v1.2.3