diff options
Diffstat (limited to 'dev/translator-vm.js')
-rw-r--r-- | dev/translator-vm.js | 84 |
1 files changed, 33 insertions, 51 deletions
diff --git a/dev/translator-vm.js b/dev/translator-vm.js index 2a51ab8c..9f14523e 100644 --- a/dev/translator-vm.js +++ b/dev/translator-vm.js @@ -16,19 +16,32 @@ * along with this program. If not, see <https://www.gnu.org/licenses/>. */ -const fs = require('fs'); -const path = require('path'); -const assert = require('assert'); -const {DatabaseVM, DatabaseVMDictionaryImporterMediaLoader} = require('./database-vm'); -const {createDictionaryArchive} = require('./util'); - -function clone(value) { - return JSON.parse(JSON.stringify(value)); -} +import fs from 'fs'; +import url, {fileURLToPath} from 'node:url'; +import path from 'path'; +import {expect, vi} from 'vitest'; +import {AnkiNoteDataCreator} from '../ext/js/data/sandbox/anki-note-data-creator.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'; +import {JapaneseUtil} from '../ext/js/language/sandbox/japanese-util.js'; +import {Translator} from '../ext/js/language/translator.js'; +import {createDictionaryArchive} from './util.js'; + +vi.mock('../ext/js/language/dictionary-importer-media-loader.js'); + +const dirname = path.dirname(fileURLToPath(import.meta.url)); + +export class TranslatorVM { + constructor() { + global.chrome = { + runtime: { + getURL: (path2) => { + return url.pathToFileURL(path.join(dirname, '..', 'ext', path2.replace(/^\//, ''))).href; + } + } + }; -class TranslatorVM extends DatabaseVM { - constructor(globals) { - super(globals); this._japaneseUtil = null; this._translator = null; this._ankiNoteDataCreator = null; @@ -40,43 +53,14 @@ class TranslatorVM extends DatabaseVM { } async prepare(dictionaryDirectory, dictionaryName) { - this.execute([ - 'js/core.js', - 'js/data/sandbox/anki-note-data-creator.js', - 'js/data/database.js', - 'js/data/json-schema.js', - 'js/general/cache-map.js', - 'js/general/regex-util.js', - 'js/general/text-source-map.js', - 'js/language/deinflector.js', - 'js/language/sandbox/dictionary-data-util.js', - 'js/language/dictionary-importer.js', - 'js/language/dictionary-database.js', - 'js/language/sandbox/japanese-util.js', - 'js/language/translator.js', - 'js/media/media-util.js' - ]); - const [ - DictionaryImporter, - DictionaryDatabase, - JapaneseUtil, - Translator, - AnkiNoteDataCreator - ] = this.get([ - 'DictionaryImporter', - 'DictionaryDatabase', - 'JapaneseUtil', - 'Translator', - 'AnkiNoteDataCreator' - ]); - // Dictionary this._dictionaryName = dictionaryName; const testDictionary = createDictionaryArchive(dictionaryDirectory, dictionaryName); + // const testDictionaryContent = await testDictionary.arrayBuffer(); const testDictionaryContent = await testDictionary.generateAsync({type: 'arraybuffer'}); // Setup database - const dictionaryImporterMediaLoader = new DatabaseVMDictionaryImporterMediaLoader(); + const dictionaryImporterMediaLoader = new DictionaryImporterMediaLoader(); const dictionaryImporter = new DictionaryImporter(dictionaryImporterMediaLoader, null); const dictionaryDatabase = new DictionaryDatabase(); await dictionaryDatabase.prepare(); @@ -87,7 +71,9 @@ class TranslatorVM extends DatabaseVM { {prefixWildcardsSupported: true} ); - assert.deepStrictEqual(errors.length, 0); + expect(errors.length).toEqual(0); + + const myDirname = path.dirname(fileURLToPath(import.meta.url)); // Setup translator this._japaneseUtil = new JapaneseUtil(null); @@ -95,7 +81,7 @@ class TranslatorVM extends DatabaseVM { japaneseUtil: this._japaneseUtil, database: dictionaryDatabase }); - const deinflectionReasons = JSON.parse(fs.readFileSync(path.join(__dirname, '..', 'ext', 'data/deinflect.json'))); + const deinflectionReasons = JSON.parse(fs.readFileSync(path.join(myDirname, '..', 'ext', 'data/deinflect.json'))); this._translator.prepare(deinflectionReasons); // Assign properties @@ -132,10 +118,10 @@ class TranslatorVM extends DatabaseVM { if (!Object.prototype.hasOwnProperty.call(optionsPresets, entry)) { throw new Error('Invalid options preset'); } - Object.assign(options, clone(optionsPresets[entry])); + Object.assign(options, structuredClone(optionsPresets[entry])); break; case 'object': - Object.assign(options, clone(entry)); + Object.assign(options, structuredClone(entry)); break; default: throw new Error('Invalid options type'); @@ -177,7 +163,3 @@ class TranslatorVM extends DatabaseVM { return options; } } - -module.exports = { - TranslatorVM -}; |