From a0a6291db8e2be29c4ed13645c250201b4552b9d Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Wed, 29 Nov 2023 21:04:27 -0500 Subject: Update tests --- test/anki-note-builder.test.js | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) (limited to 'test/anki-note-builder.test.js') diff --git a/test/anki-note-builder.test.js b/test/anki-note-builder.test.js index e21aa993..e4af7943 100644 --- a/test/anki-note-builder.test.js +++ b/test/anki-note-builder.test.js @@ -26,7 +26,11 @@ import {TranslatorVM} from '../dev/translator-vm.js'; import {AnkiNoteBuilder} from '../ext/js/data/anki-note-builder.js'; import {JapaneseUtil} from '../ext/js/language/sandbox/japanese-util.js'; -vi.stubGlobal('fetch', async (url2) => { +/** + * @param {string} url2 + * @returns {Promise} + */ +async function fetch(url2) { const extDir = path.join(__dirname, '..', 'ext'); let filePath; try { @@ -43,11 +47,15 @@ vi.stubGlobal('fetch', async (url2) => { 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'); const dirname = path.dirname(fileURLToPath(import.meta.url)); +/** + * @returns {Promise} + */ async function createVM() { const dictionaryDirectory = path.join(dirname, 'data', 'dictionaries', 'valid-dictionary1'); const vm = new TranslatorVM(); @@ -57,6 +65,10 @@ async function createVM() { return vm; } +/** + * @param {'terms'|'kanji'} type + * @returns {string[]} + */ function getFieldMarkers(type) { switch (type) { case 'terms': @@ -117,8 +129,17 @@ function getFieldMarkers(type) { } } +/** + * @param {import('dictionary').DictionaryEntry[]} dictionaryEntries + * @param {'terms'|'kanji'} type + * @param {import('settings').ResultOutputMode} mode + * @param {string} template + * @param {import('@vitest/expect').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}}`]); @@ -151,9 +172,10 @@ async function getRenderResults(dictionaryEntries, type, mode, template, expect) query: 'query', fullQuery: 'fullQuery' }; - const {note: {fields: noteFields}, errors} = await ankiNoteBuilder.createNote({ + /** @type {import('anki-note-builder').CreateNoteDetails} */ + const details = { dictionaryEntry, - mode: null, + mode: 'test', context, template, deckName: 'deckName', @@ -165,8 +187,11 @@ async function getRenderResults(dictionaryEntries, type, mode, template, expect) duplicateScopeCheckAllModels: false, resultOutputMode: mode, glossaryLayoutMode: 'default', - compactTags: false - }); + compactTags: false, + requirements: [], + mediaOptions: null + }; + const {note: {fields: noteFields}, errors} = await ankiNoteBuilder.createNote(details); for (const error of errors) { console.error(error); } @@ -178,6 +203,7 @@ async function getRenderResults(dictionaryEntries, type, mode, template, expect) } +/** */ async function main() { const vm = await createVM(); @@ -199,6 +225,7 @@ async function main() { 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; @@ -209,9 +236,10 @@ async function main() { 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', null, template, expect)); + const results = structuredClone(await getRenderResults(dictionaryEntries, 'kanji', 'split', template, expect)); actualResults1.push({name, results}); expect(results).toStrictEqual(expected1.results); } -- cgit v1.2.3 From 338d210b66006189a0b775e8bd524c1cc11c2d9a Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 30 Nov 2023 20:00:46 -0500 Subject: Update paths --- dev/dictionary-validate.js | 5 ++++- dev/generate-css-json.js | 15 +++++++++------ test/anki-note-builder.test.js | 6 +++--- test/database.test.js | 5 ++++- test/deinflector.test.js | 5 ++++- test/dom-text-scanner.test.js | 4 +++- 6 files changed, 27 insertions(+), 13 deletions(-) (limited to 'test/anki-note-builder.test.js') diff --git a/dev/dictionary-validate.js b/dev/dictionary-validate.js index b3654e75..a6948bfe 100644 --- a/dev/dictionary-validate.js +++ b/dev/dictionary-validate.js @@ -20,14 +20,17 @@ import fs from 'fs'; import JSZip from 'jszip'; import path from 'path'; import {performance} from 'perf_hooks'; +import {fileURLToPath} from 'url'; import {createJsonSchema} from './schema-validate.js'; +const dirname = path.dirname(fileURLToPath(import.meta.url)); + /** * @param {string} relativeFileName * @returns {import('dev/dictionary-validate').Schema} */ function readSchema(relativeFileName) { - const fileName = path.join(__dirname, relativeFileName); + const fileName = path.join(dirname, relativeFileName); const source = fs.readFileSync(fileName, {encoding: 'utf8'}); return JSON.parse(source); } diff --git a/dev/generate-css-json.js b/dev/generate-css-json.js index 02e54530..e5d4d7f0 100644 --- a/dev/generate-css-json.js +++ b/dev/generate-css-json.js @@ -19,6 +19,9 @@ import css from 'css'; import fs from 'fs'; import path from 'path'; +import {fileURLToPath} from 'url'; + +const dirname = path.dirname(fileURLToPath(import.meta.url)); /** * @returns {{cssFile: string, overridesCssFile: string, outputPath: string}[]} @@ -26,14 +29,14 @@ import path from 'path'; export function getTargets() { return [ { - cssFile: path.join(__dirname, '..', 'ext/css/structured-content.css'), - overridesCssFile: path.join(__dirname, 'data/structured-content-overrides.css'), - outputPath: path.join(__dirname, '..', 'ext/data/structured-content-style.json') + cssFile: path.join(dirname, '..', 'ext/css/structured-content.css'), + overridesCssFile: path.join(dirname, 'data/structured-content-overrides.css'), + outputPath: path.join(dirname, '..', 'ext/data/structured-content-style.json') }, { - cssFile: path.join(__dirname, '..', 'ext/css/display-pronunciation.css'), - overridesCssFile: path.join(__dirname, 'data/display-pronunciation-overrides.css'), - outputPath: path.join(__dirname, '..', 'ext/data/pronunciation-style.json') + cssFile: path.join(dirname, '..', 'ext/css/display-pronunciation.css'), + overridesCssFile: path.join(dirname, 'data/display-pronunciation-overrides.css'), + outputPath: path.join(dirname, '..', 'ext/data/pronunciation-style.json') } ]; } diff --git a/test/anki-note-builder.test.js b/test/anki-note-builder.test.js index e4af7943..96dacab6 100644 --- a/test/anki-note-builder.test.js +++ b/test/anki-note-builder.test.js @@ -26,12 +26,14 @@ import {TranslatorVM} from '../dev/translator-vm.js'; import {AnkiNoteBuilder} from '../ext/js/data/anki-note-builder.js'; import {JapaneseUtil} from '../ext/js/language/sandbox/japanese-util.js'; +const dirname = path.dirname(fileURLToPath(import.meta.url)); + /** * @param {string} url2 * @returns {Promise} */ async function fetch(url2) { - const extDir = path.join(__dirname, '..', 'ext'); + const extDir = path.join(dirname, '..', 'ext'); let filePath; try { filePath = url.fileURLToPath(url2); @@ -51,8 +53,6 @@ async function fetch(url2) { vi.stubGlobal('fetch', fetch); vi.mock('../ext/js/templates/template-renderer-proxy.js'); -const dirname = path.dirname(fileURLToPath(import.meta.url)); - /** * @returns {Promise} */ diff --git a/test/database.test.js b/test/database.test.js index ee818467..30854d55 100644 --- a/test/database.test.js +++ b/test/database.test.js @@ -18,12 +18,15 @@ import {IDBFactory, IDBKeyRange} from 'fake-indexeddb'; import path from 'path'; +import {fileURLToPath} from 'node:url'; import {beforeEach, describe, expect, test, vi} from 'vitest'; import {createDictionaryArchive} from '../dev/util.js'; import {DictionaryDatabase} from '../ext/js/language/dictionary-database.js'; import {DictionaryImporterMediaLoader} from '../ext/js/language/dictionary-importer-media-loader.js'; import {DictionaryImporter} from '../ext/js/language/dictionary-importer.js'; +const dirname = path.dirname(fileURLToPath(import.meta.url)); + vi.stubGlobal('IDBKeyRange', IDBKeyRange); vi.mock('../ext/js/language/dictionary-importer-media-loader.js'); @@ -34,7 +37,7 @@ vi.mock('../ext/js/language/dictionary-importer-media-loader.js'); * @returns {import('jszip')} */ function createTestDictionaryArchive(dictionary, dictionaryName) { - const dictionaryDirectory = path.join(__dirname, 'data', 'dictionaries', dictionary); + const dictionaryDirectory = path.join(dirname, 'data', 'dictionaries', dictionary); return createDictionaryArchive(dictionaryDirectory, dictionaryName); } diff --git a/test/deinflector.test.js b/test/deinflector.test.js index 77184799..a69f8e56 100644 --- a/test/deinflector.test.js +++ b/test/deinflector.test.js @@ -20,6 +20,9 @@ import fs from 'fs'; import path from 'path'; import {describe, expect, test} from 'vitest'; import {Deinflector} from '../ext/js/language/deinflector.js'; +import {fileURLToPath} from 'node:url'; + +const dirname = path.dirname(fileURLToPath(import.meta.url)); /** * @param {Deinflector} deinflector @@ -924,7 +927,7 @@ function testDeinflections() { } ]; - const deinflectionReasons = JSON.parse(fs.readFileSync(path.join(__dirname, '..', 'ext', 'data/deinflect.json'), {encoding: 'utf8'})); + const deinflectionReasons = JSON.parse(fs.readFileSync(path.join(dirname, '..', 'ext', 'data/deinflect.json'), {encoding: 'utf8'})); const deinflector = new Deinflector(deinflectionReasons); describe('deinflections', () => { diff --git a/test/dom-text-scanner.test.js b/test/dom-text-scanner.test.js index 6d0c047b..30aec33e 100644 --- a/test/dom-text-scanner.test.js +++ b/test/dom-text-scanner.test.js @@ -19,9 +19,11 @@ import fs from 'fs'; import {JSDOM} from 'jsdom'; import path from 'path'; +import {fileURLToPath} from 'node:url'; import {expect, test} from 'vitest'; import {DOMTextScanner} from '../ext/js/dom/dom-text-scanner.js'; +const dirname = path.dirname(fileURLToPath(import.meta.url)); /** * @param {string} fileName @@ -188,7 +190,7 @@ async function testDomTextScanner(dom) { /** */ async function testDocument1() { - const dom = createJSDOM(path.join(__dirname, 'data', 'html', 'test-dom-text-scanner.html')); + const dom = createJSDOM(path.join(dirname, 'data', 'html', 'test-dom-text-scanner.html')); const window = dom.window; try { window.getComputedStyle = createAbsoluteGetComputedStyle(window); -- cgit v1.2.3