1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
/*
* 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 <https://www.gnu.org/licenses/>.
*/
// @vitest-environment jsdom
import 'fake-indexeddb/auto';
import fs 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 {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<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');
/**
* @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[]}
*/
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/expect').ExpectStatic} expect
* @returns {Promise<import('anki').NoteFields[]>}
*/
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 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});
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;
}
/** */
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;
}
});
}
});
}
await main();
|