summaryrefslogtreecommitdiff
path: root/test/utilities/anki.js
diff options
context:
space:
mode:
Diffstat (limited to 'test/utilities/anki.js')
-rw-r--r--test/utilities/anki.js145
1 files changed, 145 insertions, 0 deletions
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 <https://www.gnu.org/licenses/>.
*/
+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<import('anki').NoteFields[]>}
+ */
+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;
+}