2021-03-24 00:54:04 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2021 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 <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
const assert = require('assert');
|
2021-06-26 21:05:59 +00:00
|
|
|
const {JSDOM} = require('jsdom');
|
2021-03-24 00:54:04 +00:00
|
|
|
const {testMain} = require('../dev/util');
|
|
|
|
const {TranslatorVM} = require('../dev/translator-vm');
|
|
|
|
|
|
|
|
|
|
|
|
function clone(value) {
|
|
|
|
return JSON.parse(JSON.stringify(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
async function createVM() {
|
2021-06-26 21:05:59 +00:00
|
|
|
const dom = new JSDOM();
|
2021-07-03 02:46:38 +00:00
|
|
|
const {Node, NodeFilter, document} = dom.window;
|
2021-06-26 21:05:59 +00:00
|
|
|
|
2021-07-03 02:46:38 +00:00
|
|
|
const vm = new TranslatorVM({
|
|
|
|
Node,
|
|
|
|
NodeFilter,
|
|
|
|
document
|
|
|
|
});
|
2021-03-24 00:54:04 +00:00
|
|
|
|
2021-05-18 23:50:11 +00:00
|
|
|
const dictionaryDirectory = path.join(__dirname, 'data', 'dictionaries', 'valid-dictionary1');
|
2021-03-24 00:54:04 +00:00
|
|
|
await vm.prepare(dictionaryDirectory, 'Test Dictionary 2');
|
|
|
|
|
|
|
|
vm.execute([
|
|
|
|
'js/data/anki-note-builder.js',
|
|
|
|
'js/data/anki-util.js',
|
2021-07-17 21:10:25 +00:00
|
|
|
'js/dom/sandbox/css-style-applier.js',
|
2021-07-18 17:43:11 +00:00
|
|
|
'js/display/sandbox/pronunciation-generator.js',
|
2021-07-17 21:10:25 +00:00
|
|
|
'js/display/sandbox/structured-content-generator.js',
|
2021-07-18 15:20:03 +00:00
|
|
|
'js/templates/sandbox/anki-template-renderer.js',
|
2021-07-17 21:10:25 +00:00
|
|
|
'js/templates/sandbox/template-renderer.js',
|
|
|
|
'js/templates/sandbox/template-renderer-media-provider.js',
|
2021-03-24 00:54:04 +00:00
|
|
|
'lib/handlebars.min.js'
|
|
|
|
]);
|
|
|
|
|
|
|
|
const [
|
|
|
|
JapaneseUtil,
|
2021-07-03 02:46:38 +00:00
|
|
|
AnkiNoteBuilder,
|
2021-07-18 15:20:03 +00:00
|
|
|
AnkiTemplateRenderer
|
2021-03-24 00:54:04 +00:00
|
|
|
] = vm.get([
|
|
|
|
'JapaneseUtil',
|
2021-07-03 02:46:38 +00:00
|
|
|
'AnkiNoteBuilder',
|
2021-07-18 15:20:03 +00:00
|
|
|
'AnkiTemplateRenderer'
|
2021-03-24 00:54:04 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
class TemplateRendererProxy {
|
|
|
|
constructor() {
|
2021-06-28 00:30:37 +00:00
|
|
|
this._preparePromise = null;
|
2021-07-18 15:20:03 +00:00
|
|
|
this._ankiTemplateRenderer = new AnkiTemplateRenderer();
|
2021-03-24 00:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async render(template, data, type) {
|
2021-06-28 00:30:37 +00:00
|
|
|
await this._prepare();
|
2021-07-18 15:20:03 +00:00
|
|
|
return await this._ankiTemplateRenderer.templateRenderer.render(template, data, type);
|
2021-04-02 23:09:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async renderMulti(items) {
|
2021-06-28 00:30:37 +00:00
|
|
|
await this._prepare();
|
2021-07-18 15:20:03 +00:00
|
|
|
return await this._serializeMulti(this._ankiTemplateRenderer.templateRenderer.renderMulti(items));
|
2021-06-28 00:30:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_prepare() {
|
|
|
|
if (this._preparePromise === null) {
|
|
|
|
this._preparePromise = this._prepareInternal();
|
|
|
|
}
|
|
|
|
return this._preparePromise;
|
|
|
|
}
|
|
|
|
|
|
|
|
async _prepareInternal() {
|
2021-07-18 15:20:03 +00:00
|
|
|
await this._ankiTemplateRenderer.prepare();
|
2021-04-02 23:09:21 +00:00
|
|
|
}
|
|
|
|
|
2021-06-26 21:05:59 +00:00
|
|
|
_serializeError(error) {
|
|
|
|
try {
|
|
|
|
if (typeof error === 'object' && error !== null) {
|
2021-07-25 16:54:26 +00:00
|
|
|
const result = {
|
2021-06-26 21:05:59 +00:00
|
|
|
name: error.name,
|
|
|
|
message: error.message,
|
2021-07-25 16:54:26 +00:00
|
|
|
stack: error.stack
|
2021-06-26 21:05:59 +00:00
|
|
|
};
|
2021-07-25 16:54:26 +00:00
|
|
|
if (Object.prototype.hasOwnProperty.call(error, 'data')) {
|
|
|
|
result.data = error.data;
|
|
|
|
}
|
|
|
|
return result;
|
2021-06-26 21:05:59 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// NOP
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
value: error,
|
|
|
|
hasValue: true
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-04-02 23:09:21 +00:00
|
|
|
_serializeMulti(array) {
|
|
|
|
for (let i = 0, ii = array.length; i < ii; ++i) {
|
|
|
|
const value = array[i];
|
|
|
|
const {error} = value;
|
|
|
|
if (typeof error !== 'undefined') {
|
|
|
|
value.error = this._serializeError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return array;
|
2021-03-24 00:54:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
vm.set({TemplateRendererProxy});
|
|
|
|
|
2021-07-09 21:48:27 +00:00
|
|
|
return {vm, AnkiNoteBuilder, JapaneseUtil};
|
2021-03-24 00:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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',
|
2021-03-26 23:50:54 +00:00
|
|
|
'part-of-speech',
|
2021-03-24 00:54:04 +00:00
|
|
|
'pitch-accents',
|
|
|
|
'pitch-accent-graphs',
|
|
|
|
'pitch-accent-positions',
|
|
|
|
'reading',
|
|
|
|
'screenshot',
|
2021-05-18 00:18:37 +00:00
|
|
|
'search-query',
|
2021-07-08 00:00:30 +00:00
|
|
|
'selection-text',
|
2021-03-24 00:54:04 +00:00
|
|
|
'sentence',
|
2021-07-09 21:48:27 +00:00
|
|
|
'sentence-furigana',
|
2021-03-24 00:54:04 +00:00
|
|
|
'tags',
|
|
|
|
'url'
|
|
|
|
];
|
|
|
|
case 'kanji':
|
|
|
|
return [
|
|
|
|
'character',
|
|
|
|
'clipboard-image',
|
|
|
|
'clipboard-text',
|
|
|
|
'cloze-body',
|
|
|
|
'cloze-prefix',
|
|
|
|
'cloze-suffix',
|
|
|
|
'dictionary',
|
|
|
|
'document-title',
|
|
|
|
'glossary',
|
|
|
|
'kunyomi',
|
|
|
|
'onyomi',
|
|
|
|
'screenshot',
|
2021-05-18 00:18:37 +00:00
|
|
|
'search-query',
|
2021-07-08 00:00:30 +00:00
|
|
|
'selection-text',
|
2021-03-24 00:54:04 +00:00
|
|
|
'sentence',
|
2021-07-09 21:48:27 +00:00
|
|
|
'sentence-furigana',
|
2021-03-24 00:54:04 +00:00
|
|
|
'stroke-count',
|
|
|
|
'tags',
|
|
|
|
'url'
|
|
|
|
];
|
|
|
|
default:
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-09 21:48:27 +00:00
|
|
|
async function getRenderResults(dictionaryEntries, type, mode, template, AnkiNoteBuilder, JapaneseUtil, write) {
|
2021-03-24 00:54:04 +00:00
|
|
|
const markers = getFieldMarkers(type);
|
|
|
|
const fields = [];
|
|
|
|
for (const marker of markers) {
|
|
|
|
fields.push([marker, `{${marker}}`]);
|
|
|
|
}
|
|
|
|
|
2021-07-09 21:48:27 +00:00
|
|
|
const japaneseUtil = new JapaneseUtil(null);
|
2021-03-24 00:54:04 +00:00
|
|
|
const clozePrefix = 'cloze-prefix';
|
|
|
|
const clozeSuffix = 'cloze-suffix';
|
|
|
|
const results = [];
|
2021-03-25 23:55:31 +00:00
|
|
|
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;
|
|
|
|
}
|
2021-07-09 21:48:27 +00:00
|
|
|
const ankiNoteBuilder = new AnkiNoteBuilder({japaneseUtil});
|
2021-03-24 00:54:04 +00:00
|
|
|
const context = {
|
|
|
|
url: 'url:',
|
|
|
|
sentence: {
|
|
|
|
text: `${clozePrefix}${source}${clozeSuffix}`,
|
|
|
|
offset: clozePrefix.length
|
|
|
|
},
|
2021-05-18 00:18:37 +00:00
|
|
|
documentTitle: 'title',
|
|
|
|
query: 'query',
|
|
|
|
fullQuery: 'fullQuery'
|
2021-03-24 00:54:04 +00:00
|
|
|
};
|
2021-07-03 15:30:48 +00:00
|
|
|
const {note: {fields: noteFields}, errors} = await ankiNoteBuilder.createNote({
|
2021-04-04 20:22:35 +00:00
|
|
|
dictionaryEntry,
|
2021-03-24 00:54:04 +00:00
|
|
|
mode: null,
|
|
|
|
context,
|
2021-04-02 16:42:06 +00:00
|
|
|
template,
|
2021-03-24 00:54:04 +00:00
|
|
|
deckName: 'deckName',
|
|
|
|
modelName: 'modelName',
|
|
|
|
fields,
|
|
|
|
tags: ['yomichan'],
|
|
|
|
checkForDuplicates: true,
|
|
|
|
duplicateScope: 'collection',
|
2021-07-14 00:29:53 +00:00
|
|
|
duplicateScopeCheckAllModels: false,
|
2021-03-24 00:54:04 +00:00
|
|
|
resultOutputMode: mode,
|
|
|
|
glossaryLayoutMode: 'default',
|
2021-07-03 15:30:48 +00:00
|
|
|
compactTags: false
|
|
|
|
});
|
2021-03-24 00:54:04 +00:00
|
|
|
if (!write) {
|
2021-07-18 17:43:11 +00:00
|
|
|
for (const error of errors) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
2021-07-03 15:30:48 +00:00
|
|
|
assert.strictEqual(errors.length, 0);
|
2021-03-24 00:54:04 +00:00
|
|
|
}
|
|
|
|
results.push(noteFields);
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function main() {
|
|
|
|
const write = (process.argv[2] === '--write');
|
|
|
|
|
2021-07-09 21:48:27 +00:00
|
|
|
const {vm, AnkiNoteBuilder, JapaneseUtil} = await createVM();
|
2021-03-24 00:54:04 +00:00
|
|
|
|
|
|
|
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 = [];
|
|
|
|
|
2021-04-02 16:42:06 +00:00
|
|
|
const template = fs.readFileSync(path.join(__dirname, '..', 'ext', 'data/templates/default-anki-field-templates.handlebars'), {encoding: 'utf8'});
|
2021-03-24 00:54:04 +00:00
|
|
|
|
|
|
|
for (let i = 0, ii = tests.length; i < ii; ++i) {
|
|
|
|
const test = tests[i];
|
|
|
|
const expected1 = expectedResults1[i];
|
|
|
|
switch (test.func) {
|
|
|
|
case 'findTerms':
|
|
|
|
{
|
|
|
|
const {name, mode, text} = test;
|
|
|
|
const options = vm.buildOptions(optionsPresets, test.options);
|
2021-03-25 23:55:31 +00:00
|
|
|
const {dictionaryEntries} = clone(await vm.translator.findTerms(mode, text, options));
|
2021-07-09 21:48:27 +00:00
|
|
|
const results = mode !== 'simple' ? clone(await getRenderResults(dictionaryEntries, 'terms', mode, template, AnkiNoteBuilder, JapaneseUtil, write)) : null;
|
2021-03-24 00:54:04 +00:00
|
|
|
actualResults1.push({name, results});
|
|
|
|
if (!write) {
|
|
|
|
assert.deepStrictEqual(results, expected1.results);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'findKanji':
|
|
|
|
{
|
|
|
|
const {name, text} = test;
|
|
|
|
const options = vm.buildOptions(optionsPresets, test.options);
|
2021-03-25 23:55:31 +00:00
|
|
|
const dictionaryEntries = clone(await vm.translator.findKanji(text, options));
|
2021-07-09 21:48:27 +00:00
|
|
|
const results = clone(await getRenderResults(dictionaryEntries, 'kanji', null, template, AnkiNoteBuilder, JapaneseUtil, write));
|
2021-03-24 00:54:04 +00:00
|
|
|
actualResults1.push({name, results});
|
|
|
|
if (!write) {
|
|
|
|
assert.deepStrictEqual(results, expected1.results);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (write) {
|
|
|
|
// Use 2 indent instead of 4 to save a bit of file size
|
|
|
|
fs.writeFileSync(testResults1FilePath, JSON.stringify(actualResults1, null, 2), {encoding: 'utf8'});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (require.main === module) { testMain(main); }
|