From d5b1217df3fe7480fc5f58fe194f5bbf73281094 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Mon, 27 Nov 2023 19:23:17 -0500 Subject: Use import map --- test/japanese-util.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test/japanese-util.test.js') diff --git a/test/japanese-util.test.js b/test/japanese-util.test.js index 47da4ccb..ae5b1a16 100644 --- a/test/japanese-util.test.js +++ b/test/japanese-util.test.js @@ -16,10 +16,10 @@ * along with this program. If not, see . */ +import * as wanakana from 'wanakana'; import {expect, test} from 'vitest'; import {TextSourceMap} from '../ext/js/general/text-source-map.js'; import {JapaneseUtil} from '../ext/js/language/sandbox/japanese-util.js'; -import * as wanakana from '../ext/lib/wanakana.js'; const jp = new JapaneseUtil(wanakana); -- cgit v1.2.3 From 58ae2ab871591eea82895b1ab2a18753521eab1f Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Mon, 27 Nov 2023 22:07:28 -0500 Subject: Revert "Use import map" --- dev/jsconfig.json | 1 - ext/action-popup.html | 8 -------- ext/background.html | 8 -------- ext/info.html | 8 -------- ext/issues.html | 8 -------- ext/js/background/backend.js | 2 +- ext/js/background/offscreen.js | 2 +- ext/js/display/search-display-controller.js | 2 +- ext/js/display/search-main.js | 2 +- ext/js/dom/simple-dom-parser.js | 2 +- ext/js/language/dictionary-importer.js | 4 ++-- ext/js/pages/settings/backup-controller.js | 2 +- ext/js/pages/settings/popup-preview-frame.js | 2 +- ext/js/templates/sandbox/anki-template-renderer.js | 2 +- ext/js/templates/sandbox/template-renderer-media-provider.js | 2 +- ext/js/templates/sandbox/template-renderer.js | 2 +- ext/legal.html | 8 -------- ext/offscreen.html | 8 -------- ext/permissions.html | 8 -------- ext/popup-preview.html | 8 -------- ext/popup.html | 8 -------- ext/search.html | 8 -------- ext/settings.html | 8 -------- ext/template-renderer.html | 8 -------- ext/welcome.html | 8 -------- jsconfig.json | 1 - test/japanese-util.test.js | 2 +- test/jsconfig.json | 1 - 28 files changed, 13 insertions(+), 120 deletions(-) (limited to 'test/japanese-util.test.js') diff --git a/dev/jsconfig.json b/dev/jsconfig.json index d4efe694..5b1c450c 100644 --- a/dev/jsconfig.json +++ b/dev/jsconfig.json @@ -12,7 +12,6 @@ "skipLibCheck": false, "baseUrl": ".", "paths": { - "zip.js": ["@zip.js/zip.js"], "anki-templates": ["../types/ext/anki-templates"], "anki-templates-internal": ["../types/ext/anki-templates-internal"], "cache-map": ["../types/ext/cache-map"], diff --git a/ext/action-popup.html b/ext/action-popup.html index 5c6bfce4..b60e7055 100644 --- a/ext/action-popup.html +++ b/ext/action-popup.html @@ -12,14 +12,6 @@ - diff --git a/ext/background.html b/ext/background.html index 6f9ee5f6..dc88f397 100644 --- a/ext/background.html +++ b/ext/background.html @@ -12,14 +12,6 @@ - diff --git a/ext/info.html b/ext/info.html index 9e71ffd4..cb80053d 100644 --- a/ext/info.html +++ b/ext/info.html @@ -13,14 +13,6 @@ - diff --git a/ext/issues.html b/ext/issues.html index c75683dd..904fbf16 100644 --- a/ext/issues.html +++ b/ext/issues.html @@ -13,14 +13,6 @@ - diff --git a/ext/js/background/backend.js b/ext/js/background/backend.js index f1a47e7f..3eefed53 100644 --- a/ext/js/background/backend.js +++ b/ext/js/background/backend.js @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import * as wanakana from 'wanakana'; +import * as wanakana from '../../lib/wanakana.js'; import {AccessibilityController} from '../accessibility/accessibility-controller.js'; import {AnkiConnect} from '../comm/anki-connect.js'; import {ClipboardMonitor} from '../comm/clipboard-monitor.js'; diff --git a/ext/js/background/offscreen.js b/ext/js/background/offscreen.js index 8da661ad..4b57514d 100644 --- a/ext/js/background/offscreen.js +++ b/ext/js/background/offscreen.js @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import * as wanakana from 'wanakana'; +import * as wanakana from '../../lib/wanakana.js'; import {ClipboardReader} from '../comm/clipboard-reader.js'; import {invokeMessageHandler} from '../core.js'; import {ArrayBufferUtil} from '../data/sandbox/array-buffer-util.js'; diff --git a/ext/js/display/search-display-controller.js b/ext/js/display/search-display-controller.js index 98a4666b..2778c4cd 100644 --- a/ext/js/display/search-display-controller.js +++ b/ext/js/display/search-display-controller.js @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import * as wanakana from 'wanakana'; +import * as wanakana from '../../lib/wanakana.js'; import {ClipboardMonitor} from '../comm/clipboard-monitor.js'; import {EventListenerCollection, invokeMessageHandler} from '../core.js'; import {yomitan} from '../yomitan.js'; diff --git a/ext/js/display/search-main.js b/ext/js/display/search-main.js index c1445e37..c20cc135 100644 --- a/ext/js/display/search-main.js +++ b/ext/js/display/search-main.js @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import * as wanakana from 'wanakana'; +import * as wanakana from '../../lib/wanakana.js'; import {log} from '../core.js'; import {DocumentFocusController} from '../dom/document-focus-controller.js'; import {HotkeyHandler} from '../input/hotkey-handler.js'; diff --git a/ext/js/dom/simple-dom-parser.js b/ext/js/dom/simple-dom-parser.js index 7ee28d51..a1f63890 100644 --- a/ext/js/dom/simple-dom-parser.js +++ b/ext/js/dom/simple-dom-parser.js @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import * as parse5 from 'parse5'; +import * as parse5 from '../../lib/parse5.js'; /** * @augments import('simple-dom-parser').ISimpleDomParser diff --git a/ext/js/language/dictionary-importer.js b/ext/js/language/dictionary-importer.js index 5a4d7257..115e0726 100644 --- a/ext/js/language/dictionary-importer.js +++ b/ext/js/language/dictionary-importer.js @@ -16,14 +16,14 @@ * along with this program. If not, see . */ -import * as ajvSchemas0 from 'validate-schemas'; +import * as ajvSchemas0 from '../../lib/validate-schemas.js'; import { BlobWriter as BlobWriter0, TextWriter as TextWriter0, Uint8ArrayReader as Uint8ArrayReader0, ZipReader as ZipReader0, configure -} from 'zip.js'; +} from '../../lib/zip.js'; import {stringReverse} from '../core.js'; import {MediaUtil} from '../media/media-util.js'; import {ExtensionError} from '../core/extension-error.js'; diff --git a/ext/js/pages/settings/backup-controller.js b/ext/js/pages/settings/backup-controller.js index aeff2a97..52c5f418 100644 --- a/ext/js/pages/settings/backup-controller.js +++ b/ext/js/pages/settings/backup-controller.js @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import {Dexie} from 'dexie'; +import {Dexie} from '../../../lib/dexie.js'; import {isObject, log} from '../../core.js'; import {OptionsUtil} from '../../data/options-util.js'; import {ArrayBufferUtil} from '../../data/sandbox/array-buffer-util.js'; diff --git a/ext/js/pages/settings/popup-preview-frame.js b/ext/js/pages/settings/popup-preview-frame.js index dab21f4d..60d264fa 100644 --- a/ext/js/pages/settings/popup-preview-frame.js +++ b/ext/js/pages/settings/popup-preview-frame.js @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import * as wanakana from 'wanakana'; +import * as wanakana from '../../../lib/wanakana.js'; import {Frontend} from '../../app/frontend.js'; import {TextSourceRange} from '../../dom/text-source-range.js'; import {yomitan} from '../../yomitan.js'; diff --git a/ext/js/templates/sandbox/anki-template-renderer.js b/ext/js/templates/sandbox/anki-template-renderer.js index b0dc8223..9f4bf6ff 100644 --- a/ext/js/templates/sandbox/anki-template-renderer.js +++ b/ext/js/templates/sandbox/anki-template-renderer.js @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import {Handlebars} from 'handlebars'; +import {Handlebars} from '../../../lib/handlebars.js'; import {AnkiNoteDataCreator} from '../../data/sandbox/anki-note-data-creator.js'; import {PronunciationGenerator} from '../../display/sandbox/pronunciation-generator.js'; import {StructuredContentGenerator} from '../../display/sandbox/structured-content-generator.js'; diff --git a/ext/js/templates/sandbox/template-renderer-media-provider.js b/ext/js/templates/sandbox/template-renderer-media-provider.js index 23f334e1..d8a0a16d 100644 --- a/ext/js/templates/sandbox/template-renderer-media-provider.js +++ b/ext/js/templates/sandbox/template-renderer-media-provider.js @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import {Handlebars} from 'handlebars'; +import {Handlebars} from '../../../lib/handlebars.js'; export class TemplateRendererMediaProvider { constructor() { diff --git a/ext/js/templates/sandbox/template-renderer.js b/ext/js/templates/sandbox/template-renderer.js index d4aebd64..fe240b5f 100644 --- a/ext/js/templates/sandbox/template-renderer.js +++ b/ext/js/templates/sandbox/template-renderer.js @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import {Handlebars} from 'handlebars'; +import {Handlebars} from '../../../lib/handlebars.js'; import {ExtensionError} from '../../core/extension-error.js'; export class TemplateRenderer { diff --git a/ext/legal.html b/ext/legal.html index b853f3e8..94912c7e 100644 --- a/ext/legal.html +++ b/ext/legal.html @@ -14,14 +14,6 @@ - diff --git a/ext/offscreen.html b/ext/offscreen.html index cfab53ee..afb7eb44 100644 --- a/ext/offscreen.html +++ b/ext/offscreen.html @@ -12,14 +12,6 @@ - diff --git a/ext/permissions.html b/ext/permissions.html index 7baff14e..61b0d363 100644 --- a/ext/permissions.html +++ b/ext/permissions.html @@ -14,14 +14,6 @@ - diff --git a/ext/popup-preview.html b/ext/popup-preview.html index 7bd54470..15810242 100644 --- a/ext/popup-preview.html +++ b/ext/popup-preview.html @@ -13,14 +13,6 @@ - diff --git a/ext/popup.html b/ext/popup.html index 6bc8d690..30e8a8c0 100644 --- a/ext/popup.html +++ b/ext/popup.html @@ -15,14 +15,6 @@ - diff --git a/ext/search.html b/ext/search.html index 377a966a..8c595cc4 100644 --- a/ext/search.html +++ b/ext/search.html @@ -16,14 +16,6 @@ - diff --git a/ext/settings.html b/ext/settings.html index 276a7222..346cc1d7 100644 --- a/ext/settings.html +++ b/ext/settings.html @@ -14,14 +14,6 @@ - diff --git a/ext/template-renderer.html b/ext/template-renderer.html index d1747e99..116f1a0c 100644 --- a/ext/template-renderer.html +++ b/ext/template-renderer.html @@ -11,14 +11,6 @@ - diff --git a/ext/welcome.html b/ext/welcome.html index 355bbc5f..40639881 100644 --- a/ext/welcome.html +++ b/ext/welcome.html @@ -13,14 +13,6 @@ - diff --git a/jsconfig.json b/jsconfig.json index 4f316174..0f780ead 100644 --- a/jsconfig.json +++ b/jsconfig.json @@ -12,7 +12,6 @@ "skipLibCheck": false, "baseUrl": ".", "paths": { - "zip.js": ["@zip.js/zip.js"], "*": ["./types/ext/*"] }, "types": [ diff --git a/test/japanese-util.test.js b/test/japanese-util.test.js index ae5b1a16..47da4ccb 100644 --- a/test/japanese-util.test.js +++ b/test/japanese-util.test.js @@ -16,10 +16,10 @@ * along with this program. If not, see . */ -import * as wanakana from 'wanakana'; import {expect, test} from 'vitest'; import {TextSourceMap} from '../ext/js/general/text-source-map.js'; import {JapaneseUtil} from '../ext/js/language/sandbox/japanese-util.js'; +import * as wanakana from '../ext/lib/wanakana.js'; const jp = new JapaneseUtil(wanakana); diff --git a/test/jsconfig.json b/test/jsconfig.json index 934aab81..2461fda9 100644 --- a/test/jsconfig.json +++ b/test/jsconfig.json @@ -12,7 +12,6 @@ "skipLibCheck": false, "baseUrl": ".", "paths": { - "zip.js": ["@zip.js/zip.js"], "*": ["../types/ext/*"], "dev/*": ["../types/dev/*"], "test/*": ["../types/test/*"] -- cgit v1.2.3 From 01142ef3d62e388a1c952ad9ea3a01ed38b518b6 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Wed, 29 Nov 2023 22:06:38 -0500 Subject: Update types --- test/japanese-util.test.js | 39 +++- test/jsdom.test.js | 9 +- test/json-schema.test.js | 71 +++++-- test/options-util.test.js | 49 ++++- test/profile-conditions-util.test.js | 357 +++++++++++++++++++---------------- test/text-source-map.test.js | 10 +- test/translator.test.js | 5 +- 7 files changed, 347 insertions(+), 193 deletions(-) (limited to 'test/japanese-util.test.js') diff --git a/test/japanese-util.test.js b/test/japanese-util.test.js index 47da4ccb..a0078da0 100644 --- a/test/japanese-util.test.js +++ b/test/japanese-util.test.js @@ -23,8 +23,10 @@ import * as wanakana from '../ext/lib/wanakana.js'; const jp = new JapaneseUtil(wanakana); +/** */ function testIsCodePointKanji() { test('isCodePointKanji', () => { + /** @type {[characters: string, expected: boolean][]} */ const data = [ ['力方', true], ['\u53f1\u{20b9f}', true], @@ -34,7 +36,7 @@ function testIsCodePointKanji() { for (const [characters, expected] of data) { for (const character of characters) { - const codePoint = character.codePointAt(0); + const codePoint = /** @type {number} */ (character.codePointAt(0)); const actual = jp.isCodePointKanji(codePoint); expect(actual).toStrictEqual(expected); // `isCodePointKanji failed for ${character} (\\u{${codePoint.toString(16)}})` } @@ -42,8 +44,10 @@ function testIsCodePointKanji() { }); } +/** */ function testIsCodePointKana() { test('isCodePointKana', () => { + /** @type {[characters: string, expected: boolean][]} */ const data = [ ['かたカタ', true], ['力方々kata、。?,.?', false], @@ -52,7 +56,7 @@ function testIsCodePointKana() { for (const [characters, expected] of data) { for (const character of characters) { - const codePoint = character.codePointAt(0); + const codePoint = /** @type {number} */ (character.codePointAt(0)); const actual = jp.isCodePointKana(codePoint); expect(actual).toStrictEqual(expected); // `isCodePointKana failed for ${character} (\\u{${codePoint.toString(16)}})` } @@ -60,8 +64,10 @@ function testIsCodePointKana() { }); } +/** */ function testIsCodePointJapanese() { test('isCodePointJapanese', () => { + /** @type {[characters: string, expected: boolean][]} */ const data = [ ['かたカタ力方々、。?', true], ['\u53f1\u{20b9f}', true], @@ -71,7 +77,7 @@ function testIsCodePointJapanese() { for (const [characters, expected] of data) { for (const character of characters) { - const codePoint = character.codePointAt(0); + const codePoint = /** @type {number} */ (character.codePointAt(0)); const actual = jp.isCodePointJapanese(codePoint); expect(actual).toStrictEqual(expected); // `isCodePointJapanese failed for ${character} (\\u{${codePoint.toString(16)}})` } @@ -79,8 +85,10 @@ function testIsCodePointJapanese() { }); } +/** */ function testIsStringEntirelyKana() { test('isStringEntirelyKana', () => { + /** @type {[string: string, expected: boolean][]} */ const data = [ ['かたかな', true], ['カタカナ', true], @@ -101,8 +109,10 @@ function testIsStringEntirelyKana() { }); } +/** */ function testIsStringPartiallyJapanese() { test('isStringPartiallyJapanese', () => { + /** @type {[string: string, expected: boolean][]} */ const data = [ ['かたかな', true], ['カタカナ', true], @@ -124,8 +134,10 @@ function testIsStringPartiallyJapanese() { }); } +/** */ function testConvertKatakanaToHiragana() { test('convertKatakanaToHiragana', () => { + /** @type {[string: string, expected: string, keepProlongedSoundMarks?: boolean][]} */ const data = [ ['かたかな', 'かたかな'], ['ひらがな', 'ひらがな'], @@ -146,8 +158,10 @@ function testConvertKatakanaToHiragana() { }); } +/** */ function testConvertHiraganaToKatakana() { test('ConvertHiraganaToKatakana', () => { + /** @type {[string: string, expected: string][]} */ const data = [ ['かたかな', 'カタカナ'], ['ひらがな', 'ヒラガナ'], @@ -166,8 +180,10 @@ function testConvertHiraganaToKatakana() { }); } +/** */ function testConvertToRomaji() { test('ConvertToRomaji', () => { + /** @type {[string: string, expected: string][]} */ const data = [ ['かたかな', 'katakana'], ['ひらがな', 'hiragana'], @@ -186,8 +202,10 @@ function testConvertToRomaji() { }); } +/** */ function testConvertNumericToFullWidth() { test('ConvertNumericToFullWidth', () => { + /** @type {[string: string, expected: string][]} */ const data = [ ['0123456789', '0123456789'], ['abcdefghij', 'abcdefghij'], @@ -201,8 +219,10 @@ function testConvertNumericToFullWidth() { }); } +/** */ function testConvertHalfWidthKanaToFullWidth() { test('ConvertHalfWidthKanaToFullWidth', () => { + /** @type {[string: string, expected: string, expectedSourceMapping?: number[]][]} */ const data = [ ['0123456789', '0123456789'], ['abcdefghij', 'abcdefghij'], @@ -227,8 +247,10 @@ function testConvertHalfWidthKanaToFullWidth() { }); } +/** */ function testConvertAlphabeticToKana() { test('ConvertAlphabeticToKana', () => { + /** @type {[string: string, expected: string, expectedSourceMapping?: number[]][]} */ const data = [ ['0123456789', '0123456789'], ['abcdefghij', 'あbcでfgひj', [1, 1, 1, 2, 1, 1, 2, 1]], @@ -252,8 +274,10 @@ function testConvertAlphabeticToKana() { }); } +/** */ function testDistributeFurigana() { test('DistributeFurigana', () => { + /** @type {[input: [term: string, reading: string], expected: {text: string, reading: string}[]][]} */ const data = [ [ ['有り難う', 'ありがとう'], @@ -719,8 +743,10 @@ function testDistributeFurigana() { }); } +/** */ function testDistributeFuriganaInflected() { test('DistributeFuriganaInflected', () => { + /** @type {[input: [term: string, reading: string, source: string], expected: {text: string, reading: string}[]][]} */ const data = [ [ ['美味しい', 'おいしい', '美味しかた'], @@ -770,8 +796,10 @@ function testDistributeFuriganaInflected() { }); } +/** */ function testCollapseEmphaticSequences() { test('CollapseEmphaticSequences', () => { + /** @type {[input: [text: string, fullCollapse: boolean], output: [expected: string, expectedSourceMapping: number[]]][]} */ const data = [ [['かこい', false], ['かこい', [1, 1, 1]]], [['かこい', true], ['かこい', [1, 1, 1]]], @@ -825,8 +853,10 @@ function testCollapseEmphaticSequences() { }); } +/** */ function testIsMoraPitchHigh() { test('IsMoraPitchHigh', () => { + /** @type {[input: [moraIndex: number, pitchAccentDownstepPosition: number], expected: boolean][]} */ const data = [ [[0, 0], false], [[1, 0], true], @@ -861,8 +891,10 @@ function testIsMoraPitchHigh() { }); } +/** */ function testGetKanaMorae() { test('GetKanaMorae', () => { + /** @type {[text: string, expected: string[]][]} */ const data = [ ['かこ', ['か', 'こ']], ['かっこ', ['か', 'っ', 'こ']], @@ -883,6 +915,7 @@ function testGetKanaMorae() { } +/** */ function main() { testIsCodePointKanji(); testIsCodePointKana(); diff --git a/test/jsdom.test.js b/test/jsdom.test.js index c53f374e..6c2e00ee 100644 --- a/test/jsdom.test.js +++ b/test/jsdom.test.js @@ -26,20 +26,25 @@ import {expect, test} from 'vitest'; */ function testJSDOMSelectorBug() { test('JSDOMSelectorBug', () => { - // nwsapi is used by JSDOM + // nwsapi is used by JSDOM const dom = new JSDOM(); const {document} = dom.window; const div = document.createElement('div'); div.innerHTML = '
'; const c = div.querySelector('.c'); - expect(() => c.matches('.a:nth-last-of-type(1) .b .c')).not.toThrow(); + expect(() => { + if (c === null) { throw new Error('Element not found'); } + c.matches('.a:nth-last-of-type(1) .b .c'); + }).not.toThrow(); }); } +/** */ export function testJSDOM() { testJSDOMSelectorBug(); } +/** */ function main() { testJSDOM(); } diff --git a/test/json-schema.test.js b/test/json-schema.test.js index 5370e8da..e534f538 100644 --- a/test/json-schema.test.js +++ b/test/json-schema.test.js @@ -19,25 +19,47 @@ import {expect, test} from 'vitest'; import {JsonSchema} from '../ext/js/data/json-schema.js'; +/** + * @param {import('json-schema').Schema} schema + * @param {unknown} value + * @returns {boolean} + */ function schemaValidate(schema, value) { return new JsonSchema(schema).isValid(value); } +/** + * @param {import('json-schema').Schema} schema + * @param {unknown} value + * @returns {import('json-schema').Value} + */ function getValidValueOrDefault(schema, value) { return new JsonSchema(schema).getValidValueOrDefault(value); } +/** + * @param {import('json-schema').Schema} schema + * @param {import('json-schema').Value} value + * @returns {import('json-schema').Value} + */ function createProxy(schema, value) { return new JsonSchema(schema).createProxy(value); } +/** + * @template T + * @param {T} value + * @returns {T} + */ function clone(value) { return JSON.parse(JSON.stringify(value)); } +/** */ function testValidate1() { test('Validate1', () => { + /** @type {import('json-schema').Schema} */ const schema = { allOf: [ { @@ -56,28 +78,34 @@ function testValidate1() { ] }, { - not: [ - {multipleOf: 20} - ] + not: { + anyOf: [ + {multipleOf: 20} + ] + } } ] }; + /** + * @param {number} value + * @returns {boolean} + */ const jsValidate = (value) => { return ( typeof value === 'number' && - ( - (value >= 10 && value <= 100) || - (value >= -100 && value <= -10) - ) && - ( ( - (value % 3) === 0 || - (value % 5) === 0 + (value >= 10 && value <= 100) || + (value >= -100 && value <= -10) + ) && + ( + ( + (value % 3) === 0 || + (value % 5) === 0 + ) && + (value % 15) !== 0 ) && - (value % 15) !== 0 - ) && - (value % 20) !== 0 + (value % 20) !== 0 ); }; @@ -89,10 +117,12 @@ function testValidate1() { }); } +/** */ function testValidate2() { test('Validate2', () => { + /** @type {{schema: import('json-schema').Schema, inputs: {expected: boolean, value: unknown}[]}[]} */ const data = [ - // String tests + // String tests { schema: { type: 'string' @@ -494,10 +524,12 @@ function testValidate2() { } +/** */ function testGetValidValueOrDefault1() { test('GetValidValueOrDefault1', () => { + /** @type {{schema: import('json-schema').Schema, inputs: [value: unknown, expected: unknown][]}[]} */ const data = [ - // Test value defaulting on objects with additionalProperties=false + // Test value defaulting on objects with additionalProperties=false { schema: { type: 'object', @@ -667,10 +699,10 @@ function testGetValidValueOrDefault1() { type: 'object', required: ['toString'], properties: { - toString: { + toString: /** @type {import('json-schema').SchemaObject} */ ({ type: 'string', default: 'default' - } + }) } }, inputs: [ @@ -850,10 +882,12 @@ function testGetValidValueOrDefault1() { } +/** */ function testProxy1() { test('Proxy1', () => { + /** @type {{schema: import('json-schema').Schema, tests: {error: boolean, value?: import('json-schema').Value, action: (value: import('core').SafeAny) => void}[]}[]} */ const data = [ - // Object tests + // Object tests { schema: { type: 'object', @@ -998,6 +1032,7 @@ function testProxy1() { } +/** */ function main() { testValidate1(); testValidate2(); diff --git a/test/options-util.test.js b/test/options-util.test.js index ff5b6713..7845d759 100644 --- a/test/options-util.test.js +++ b/test/options-util.test.js @@ -24,7 +24,12 @@ import {OptionsUtil} from '../ext/js/data/options-util.js'; import {TemplatePatcher} from '../ext/js/templates/template-patcher.js'; const dirname = path.dirname(fileURLToPath(import.meta.url)); -vi.stubGlobal('fetch', async function fetch(url2) { + +/** + * @param {string} url2 + * @returns {Promise} + */ +async function fetch(url2) { const filePath = url.fileURLToPath(url2); await Promise.resolve(); const content = fs.readFileSync(filePath, {encoding: null}); @@ -35,15 +40,22 @@ vi.stubGlobal('fetch', async function fetch(url2) { text: async () => Promise.resolve(content.toString('utf8')), json: async () => Promise.resolve(JSON.parse(content.toString('utf8'))) }; -}); -vi.stubGlobal('chrome', { +} +/** @type {import('dev/vm').PseudoChrome} */ +const chrome = { runtime: { - getURL: (path2) => { + getURL(path2) { return url.pathToFileURL(path.join(dirname, '..', 'ext', path2.replace(/^\//, ''))).href; } } -}); +}; + +vi.stubGlobal('fetch', fetch); +vi.stubGlobal('chrome', chrome); +/** + * @returns {unknown} + */ function createProfileOptionsTestData1() { return { version: 14, @@ -144,6 +156,9 @@ function createProfileOptionsTestData1() { }; } +/** + * @returns {unknown} + */ function createOptionsTestData1() { return { profiles: [ @@ -243,6 +258,9 @@ function createOptionsTestData1() { } +/** + * @returns {unknown} + */ function createProfileOptionsUpdatedTestData1() { return { general: { @@ -519,6 +537,9 @@ function createProfileOptionsUpdatedTestData1() { }; } +/** + * @returns {unknown} + */ function createOptionsUpdatedTestData1() { return { profiles: [ @@ -612,6 +633,7 @@ function createOptionsUpdatedTestData1() { } +/** */ async function testUpdate() { test('Update', async () => { const optionsUtil = new OptionsUtil(); @@ -624,8 +646,10 @@ async function testUpdate() { }); } +/** */ async function testDefault() { test('Default', async () => { + /** @type {((options: import('options-util').IntermediateOptions) => void)[]} */ const data = [ (options) => options, (options) => { @@ -651,12 +675,17 @@ async function testDefault() { }); } +/** */ async function testFieldTemplatesUpdate() { test('FieldTemplatesUpdate', async () => { const optionsUtil = new OptionsUtil(); await optionsUtil.prepare(); const templatePatcher = new TemplatePatcher(); + /** + * @param {string} fileName + * @returns {string} + */ const loadDataFile = (fileName) => { const content = fs.readFileSync(path.join(dirname, '..', 'ext', fileName), {encoding: 'utf8'}); return templatePatcher.parsePatch(content).addition; @@ -671,6 +700,11 @@ async function testFieldTemplatesUpdate() { {version: 13, changes: loadDataFile('data/templates/anki-field-templates-upgrade-v13.handlebars')}, {version: 21, changes: loadDataFile('data/templates/anki-field-templates-upgrade-v21.handlebars')} ]; + /** + * @param {number} startVersion + * @param {number} targetVersion + * @returns {string} + */ const getUpdateAdditions = (startVersion, targetVersion) => { let value = ''; for (const {version, changes} of updates) { @@ -682,7 +716,7 @@ async function testFieldTemplatesUpdate() { }; const data = [ - // Standard format + // Standard format { oldVersion: 0, newVersion: 12, @@ -1564,7 +1598,7 @@ async function testFieldTemplatesUpdate() { const updatesPattern = /<<>>/g; for (const {old, expected, oldVersion, newVersion} of data) { - const options = createOptionsTestData1(); + const options = /** @type {import('core').SafeAny} */ (createOptionsTestData1()); options.profiles[0].options.anki.fieldTemplates = old; options.version = oldVersion; @@ -1578,6 +1612,7 @@ async function testFieldTemplatesUpdate() { } +/** */ async function main() { await testUpdate(); await testDefault(); diff --git a/test/profile-conditions-util.test.js b/test/profile-conditions-util.test.js index ca8b00ef..30052b34 100644 --- a/test/profile-conditions-util.test.js +++ b/test/profile-conditions-util.test.js @@ -19,31 +19,33 @@ import {expect, test} from 'vitest'; import {ProfileConditionsUtil} from '../ext/js/background/profile-conditions-util.js'; +/** */ function testNormalizeContext() { test('NormalizeContext', () => { + /** @type {{context: import('settings').OptionsContext, expected: import('profile-conditions-util').NormalizedOptionsContext}[]} */ const data = [ - // Empty + // Empty { - context: {}, - expected: {flags: []} + context: {index: 0}, + expected: {index: 0, flags: []} }, // Domain normalization { - context: {url: ''}, - expected: {url: '', flags: []} + context: {depth: 0, url: ''}, + expected: {depth: 0, url: '', flags: []} }, { - context: {url: 'http://example.com/'}, - expected: {url: 'http://example.com/', domain: 'example.com', flags: []} + context: {depth: 0, url: 'http://example.com/'}, + expected: {depth: 0, url: 'http://example.com/', domain: 'example.com', flags: []} }, { - context: {url: 'http://example.com:1234/'}, - expected: {url: 'http://example.com:1234/', domain: 'example.com', flags: []} + context: {depth: 0, url: 'http://example.com:1234/'}, + expected: {depth: 0, url: 'http://example.com:1234/', domain: 'example.com', flags: []} }, { - context: {url: 'http://user@example.com:1234/'}, - expected: {url: 'http://user@example.com:1234/', domain: 'example.com', flags: []} + context: {depth: 0, url: 'http://user@example.com:1234/'}, + expected: {depth: 0, url: 'http://user@example.com:1234/', domain: 'example.com', flags: []} } ]; @@ -55,15 +57,17 @@ function testNormalizeContext() { }); } +/** */ function testSchemas() { test('Schemas', () => { + /** @type {{conditionGroups: import('settings').ProfileConditionGroup[], expectedSchema?: import('json-schema').Schema, inputs?: {expected: boolean, context: import('settings').OptionsContext}[]}[]} */ const data = [ - // Empty + // Empty { conditionGroups: [], expectedSchema: {}, inputs: [ - {expected: true, context: {url: 'http://example.com/'}} + {expected: true, context: {depth: 0, url: 'http://example.com/'}} ] }, { @@ -72,7 +76,7 @@ function testSchemas() { ], expectedSchema: {}, inputs: [ - {expected: true, context: {url: 'http://example.com/'}} + {expected: true, context: {depth: 0, url: 'http://example.com/'}} ] }, { @@ -82,7 +86,7 @@ function testSchemas() { ], expectedSchema: {}, inputs: [ - {expected: true, context: {url: 'http://example.com/'}} + {expected: true, context: {depth: 0, url: 'http://example.com/'}} ] }, @@ -124,14 +128,16 @@ function testSchemas() { } ], expectedSchema: { - not: [ - { - properties: { - depth: {const: 0} - }, - required: ['depth'] - } - ] + not: { + anyOf: [ + { + properties: { + depth: {const: 0} + }, + required: ['depth'] + } + ] + } }, inputs: [ {expected: false, context: {depth: 0, url: 'http://example.com/'}}, @@ -371,9 +377,9 @@ function testSchemas() { }, inputs: [ {expected: true, context: {depth: 0, url: 'http://example.com/', modifierKeys: []}}, - {expected: false, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['Alt']}}, - {expected: false, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['Alt', 'Shift']}}, - {expected: false, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['Alt', 'Shift', 'Ctrl']}} + {expected: false, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['alt']}}, + {expected: false, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['alt', 'shift']}}, + {expected: false, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['alt', 'shift', 'ctrl']}} ] }, { @@ -383,7 +389,7 @@ function testSchemas() { { type: 'modifierKeys', operator: 'are', - value: 'Alt, Shift' + value: 'alt, shift' } ] } @@ -395,8 +401,8 @@ function testSchemas() { maxItems: 2, minItems: 2, allOf: [ - {contains: {const: 'Alt'}}, - {contains: {const: 'Shift'}} + {contains: {const: 'alt'}}, + {contains: {const: 'shift'}} ] } }, @@ -404,9 +410,9 @@ function testSchemas() { }, inputs: [ {expected: false, context: {depth: 0, url: 'http://example.com/', modifierKeys: []}}, - {expected: false, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['Alt']}}, - {expected: true, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['Alt', 'Shift']}}, - {expected: false, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['Alt', 'Shift', 'Ctrl']}} + {expected: false, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['alt']}}, + {expected: true, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['alt', 'shift']}}, + {expected: false, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['alt', 'shift', 'ctrl']}} ] }, { @@ -422,24 +428,26 @@ function testSchemas() { } ], expectedSchema: { - not: [ - { - properties: { - modifierKeys: { - type: 'array', - maxItems: 0, - minItems: 0 - } - }, - required: ['modifierKeys'] - } - ] + not: { + anyOf: [ + { + properties: { + modifierKeys: { + type: 'array', + maxItems: 0, + minItems: 0 + } + }, + required: ['modifierKeys'] + } + ] + } }, inputs: [ {expected: false, context: {depth: 0, url: 'http://example.com/', modifierKeys: []}}, - {expected: true, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['Alt']}}, - {expected: true, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['Alt', 'Shift']}}, - {expected: true, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['Alt', 'Shift', 'Ctrl']}} + {expected: true, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['alt']}}, + {expected: true, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['alt', 'shift']}}, + {expected: true, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['alt', 'shift', 'ctrl']}} ] }, { @@ -449,34 +457,36 @@ function testSchemas() { { type: 'modifierKeys', operator: 'areNot', - value: 'Alt, Shift' + value: 'alt, shift' } ] } ], expectedSchema: { - not: [ - { - properties: { - modifierKeys: { - type: 'array', - maxItems: 2, - minItems: 2, - allOf: [ - {contains: {const: 'Alt'}}, - {contains: {const: 'Shift'}} - ] - } - }, - required: ['modifierKeys'] - } - ] + not: { + anyOf: [ + { + properties: { + modifierKeys: { + type: 'array', + maxItems: 2, + minItems: 2, + allOf: [ + {contains: {const: 'alt'}}, + {contains: {const: 'shift'}} + ] + } + }, + required: ['modifierKeys'] + } + ] + } }, inputs: [ {expected: true, context: {depth: 0, url: 'http://example.com/', modifierKeys: []}}, - {expected: true, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['Alt']}}, - {expected: false, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['Alt', 'Shift']}}, - {expected: true, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['Alt', 'Shift', 'Ctrl']}} + {expected: true, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['alt']}}, + {expected: false, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['alt', 'shift']}}, + {expected: true, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['alt', 'shift', 'ctrl']}} ] }, { @@ -502,9 +512,9 @@ function testSchemas() { }, inputs: [ {expected: true, context: {depth: 0, url: 'http://example.com/', modifierKeys: []}}, - {expected: true, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['Alt']}}, - {expected: true, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['Alt', 'Shift']}}, - {expected: true, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['Alt', 'Shift', 'Ctrl']}} + {expected: true, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['alt']}}, + {expected: true, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['alt', 'shift']}}, + {expected: true, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['alt', 'shift', 'ctrl']}} ] }, { @@ -514,7 +524,7 @@ function testSchemas() { { type: 'modifierKeys', operator: 'include', - value: 'Alt, Shift' + value: 'alt, shift' } ] } @@ -525,8 +535,8 @@ function testSchemas() { type: 'array', minItems: 2, allOf: [ - {contains: {const: 'Alt'}}, - {contains: {const: 'Shift'}} + {contains: {const: 'alt'}}, + {contains: {const: 'shift'}} ] } }, @@ -534,9 +544,9 @@ function testSchemas() { }, inputs: [ {expected: false, context: {depth: 0, url: 'http://example.com/', modifierKeys: []}}, - {expected: false, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['Alt']}}, - {expected: true, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['Alt', 'Shift']}}, - {expected: true, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['Alt', 'Shift', 'Ctrl']}} + {expected: false, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['alt']}}, + {expected: true, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['alt', 'shift']}}, + {expected: true, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['alt', 'shift', 'ctrl']}} ] }, { @@ -561,9 +571,9 @@ function testSchemas() { }, inputs: [ {expected: true, context: {depth: 0, url: 'http://example.com/', modifierKeys: []}}, - {expected: true, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['Alt']}}, - {expected: true, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['Alt', 'Shift']}}, - {expected: true, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['Alt', 'Shift', 'Ctrl']}} + {expected: true, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['alt']}}, + {expected: true, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['alt', 'shift']}}, + {expected: true, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['alt', 'shift', 'ctrl']}} ] }, { @@ -573,7 +583,7 @@ function testSchemas() { { type: 'modifierKeys', operator: 'notInclude', - value: 'Alt, Shift' + value: 'alt, shift' } ] } @@ -582,19 +592,21 @@ function testSchemas() { properties: { modifierKeys: { type: 'array', - not: [ - {contains: {const: 'Alt'}}, - {contains: {const: 'Shift'}} - ] + not: { + anyOf: [ + {contains: {const: 'alt'}}, + {contains: {const: 'shift'}} + ] + } } }, required: ['modifierKeys'] }, inputs: [ {expected: true, context: {depth: 0, url: 'http://example.com/', modifierKeys: []}}, - {expected: false, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['Alt']}}, - {expected: false, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['Alt', 'Shift']}}, - {expected: false, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['Alt', 'Shift', 'Ctrl']}} + {expected: false, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['alt']}}, + {expected: false, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['alt', 'shift']}}, + {expected: false, context: {depth: 0, url: 'http://example.com/', modifierKeys: ['alt', 'shift', 'ctrl']}} ] }, @@ -622,11 +634,13 @@ function testSchemas() { } }, inputs: [ - {expected: true, context: {}}, - {expected: true, context: {flags: []}}, - {expected: false, context: {flags: ['test1']}}, - {expected: false, context: {flags: ['test1', 'test2']}}, - {expected: false, context: {flags: ['test1', 'test2', 'test3']}} + {expected: true, context: {depth: 0, url: ''}}, + {expected: true, context: {depth: 0, url: '', flags: []}}, + {expected: false, context: {depth: 0, url: '', flags: ['clipboard']}}, + // @ts-ignore - Ignore type for string flag for testing purposes + {expected: false, context: {depth: 0, url: '', flags: ['clipboard', 'test2']}}, + // @ts-ignore - Ignore type for string flag for testing purposes + {expected: false, context: {depth: 0, url: '', flags: ['clipboard', 'test2', 'test3']}} ] }, { @@ -636,7 +650,7 @@ function testSchemas() { { type: 'flags', operator: 'are', - value: 'test1, test2' + value: 'clipboard, test2' } ] } @@ -649,18 +663,20 @@ function testSchemas() { maxItems: 2, minItems: 2, allOf: [ - {contains: {const: 'test1'}}, + {contains: {const: 'clipboard'}}, {contains: {const: 'test2'}} ] } } }, inputs: [ - {expected: false, context: {}}, - {expected: false, context: {flags: []}}, - {expected: false, context: {flags: ['test1']}}, - {expected: true, context: {flags: ['test1', 'test2']}}, - {expected: false, context: {flags: ['test1', 'test2', 'test3']}} + {expected: false, context: {depth: 0, url: ''}}, + {expected: false, context: {depth: 0, url: '', flags: []}}, + {expected: false, context: {depth: 0, url: '', flags: ['clipboard']}}, + // @ts-ignore - Ignore type for string flag for testing purposes + {expected: true, context: {depth: 0, url: '', flags: ['clipboard', 'test2']}}, + // @ts-ignore - Ignore type for string flag for testing purposes + {expected: false, context: {depth: 0, url: '', flags: ['clipboard', 'test2', 'test3']}} ] }, { @@ -676,25 +692,29 @@ function testSchemas() { } ], expectedSchema: { - not: [ - { - required: ['flags'], - properties: { - flags: { - type: 'array', - maxItems: 0, - minItems: 0 + not: { + anyOf: [ + { + required: ['flags'], + properties: { + flags: { + type: 'array', + maxItems: 0, + minItems: 0 + } } } - } - ] + ] + } }, inputs: [ - {expected: false, context: {}}, - {expected: false, context: {flags: []}}, - {expected: true, context: {flags: ['test1']}}, - {expected: true, context: {flags: ['test1', 'test2']}}, - {expected: true, context: {flags: ['test1', 'test2', 'test3']}} + {expected: false, context: {depth: 0, url: ''}}, + {expected: false, context: {depth: 0, url: '', flags: []}}, + {expected: true, context: {depth: 0, url: '', flags: ['clipboard']}}, + // @ts-ignore - Ignore type for string flag for testing purposes + {expected: true, context: {depth: 0, url: '', flags: ['clipboard', 'test2']}}, + // @ts-ignore - Ignore type for string flag for testing purposes + {expected: true, context: {depth: 0, url: '', flags: ['clipboard', 'test2', 'test3']}} ] }, { @@ -704,35 +724,39 @@ function testSchemas() { { type: 'flags', operator: 'areNot', - value: 'test1, test2' + value: 'clipboard, test2' } ] } ], expectedSchema: { - not: [ - { - required: ['flags'], - properties: { - flags: { - type: 'array', - maxItems: 2, - minItems: 2, - allOf: [ - {contains: {const: 'test1'}}, - {contains: {const: 'test2'}} - ] + not: { + anyOf: [ + { + required: ['flags'], + properties: { + flags: { + type: 'array', + maxItems: 2, + minItems: 2, + allOf: [ + {contains: {const: 'clipboard'}}, + {contains: {const: 'test2'}} + ] + } } } - } - ] + ] + } }, inputs: [ - {expected: true, context: {}}, - {expected: true, context: {flags: []}}, - {expected: true, context: {flags: ['test1']}}, - {expected: false, context: {flags: ['test1', 'test2']}}, - {expected: true, context: {flags: ['test1', 'test2', 'test3']}} + {expected: true, context: {depth: 0, url: ''}}, + {expected: true, context: {depth: 0, url: '', flags: []}}, + {expected: true, context: {depth: 0, url: '', flags: ['clipboard']}}, + // @ts-ignore - Ignore type for string flag for testing purposes + {expected: false, context: {depth: 0, url: '', flags: ['clipboard', 'test2']}}, + // @ts-ignore - Ignore type for string flag for testing purposes + {expected: true, context: {depth: 0, url: '', flags: ['clipboard', 'test2', 'test3']}} ] }, { @@ -757,11 +781,13 @@ function testSchemas() { } }, inputs: [ - {expected: true, context: {}}, - {expected: true, context: {flags: []}}, - {expected: true, context: {flags: ['test1']}}, - {expected: true, context: {flags: ['test1', 'test2']}}, - {expected: true, context: {flags: ['test1', 'test2', 'test3']}} + {expected: true, context: {depth: 0, url: ''}}, + {expected: true, context: {depth: 0, url: '', flags: []}}, + {expected: true, context: {depth: 0, url: '', flags: ['clipboard']}}, + // @ts-ignore - Ignore type for string flag for testing purposes + {expected: true, context: {depth: 0, url: '', flags: ['clipboard', 'test2']}}, + // @ts-ignore - Ignore type for string flag for testing purposes + {expected: true, context: {depth: 0, url: '', flags: ['clipboard', 'test2', 'test3']}} ] }, { @@ -771,7 +797,7 @@ function testSchemas() { { type: 'flags', operator: 'include', - value: 'test1, test2' + value: 'clipboard, test2' } ] } @@ -783,18 +809,20 @@ function testSchemas() { type: 'array', minItems: 2, allOf: [ - {contains: {const: 'test1'}}, + {contains: {const: 'clipboard'}}, {contains: {const: 'test2'}} ] } } }, inputs: [ - {expected: false, context: {}}, - {expected: false, context: {flags: []}}, - {expected: false, context: {flags: ['test1']}}, - {expected: true, context: {flags: ['test1', 'test2']}}, - {expected: true, context: {flags: ['test1', 'test2', 'test3']}} + {expected: false, context: {depth: 0, url: ''}}, + {expected: false, context: {depth: 0, url: '', flags: []}}, + {expected: false, context: {depth: 0, url: '', flags: ['clipboard']}}, + // @ts-ignore - Ignore type for string flag for testing purposes + {expected: true, context: {depth: 0, url: '', flags: ['clipboard', 'test2']}}, + // @ts-ignore - Ignore type for string flag for testing purposes + {expected: true, context: {depth: 0, url: '', flags: ['clipboard', 'test2', 'test3']}} ] }, { @@ -818,11 +846,13 @@ function testSchemas() { } }, inputs: [ - {expected: true, context: {}}, - {expected: true, context: {flags: []}}, - {expected: true, context: {flags: ['test1']}}, - {expected: true, context: {flags: ['test1', 'test2']}}, - {expected: true, context: {flags: ['test1', 'test2', 'test3']}} + {expected: true, context: {depth: 0, url: ''}}, + {expected: true, context: {depth: 0, url: '', flags: []}}, + {expected: true, context: {depth: 0, url: '', flags: ['clipboard']}}, + // @ts-ignore - Ignore type for string flag for testing purposes + {expected: true, context: {depth: 0, url: '', flags: ['clipboard', 'test2']}}, + // @ts-ignore - Ignore type for string flag for testing purposes + {expected: true, context: {depth: 0, url: '', flags: ['clipboard', 'test2', 'test3']}} ] }, { @@ -832,7 +862,7 @@ function testSchemas() { { type: 'flags', operator: 'notInclude', - value: 'test1, test2' + value: 'clipboard, test2' } ] } @@ -842,19 +872,23 @@ function testSchemas() { properties: { flags: { type: 'array', - not: [ - {contains: {const: 'test1'}}, - {contains: {const: 'test2'}} - ] + not: { + anyOf: [ + {contains: {const: 'clipboard'}}, + {contains: {const: 'test2'}} + ] + } } } }, inputs: [ - {expected: true, context: {}}, - {expected: true, context: {flags: []}}, - {expected: false, context: {flags: ['test1']}}, - {expected: false, context: {flags: ['test1', 'test2']}}, - {expected: false, context: {flags: ['test1', 'test2', 'test3']}} + {expected: true, context: {depth: 0, url: ''}}, + {expected: true, context: {depth: 0, url: '', flags: []}}, + {expected: false, context: {depth: 0, url: '', flags: ['clipboard']}}, + // @ts-ignore - Ignore type for string flag for testing purposes + {expected: false, context: {depth: 0, url: '', flags: ['clipboard', 'test2']}}, + // @ts-ignore - Ignore type for string flag for testing purposes + {expected: false, context: {depth: 0, url: '', flags: ['clipboard', 'test2', 'test3']}} ] }, @@ -1082,6 +1116,7 @@ function testSchemas() { } +/** */ function main() { testNormalizeContext(); testSchemas(); diff --git a/test/text-source-map.test.js b/test/text-source-map.test.js index aeaba000..54b39319 100644 --- a/test/text-source-map.test.js +++ b/test/text-source-map.test.js @@ -19,6 +19,7 @@ import {expect, test} from 'vitest'; import {TextSourceMap} from '../ext/js/general/text-source-map.js'; +/** */ function testSource() { test('Source', () => { const data = [ @@ -34,8 +35,10 @@ function testSource() { }); } +/** */ function testEquals() { test('Equals', () => { + /** @type {[args1: [source1: string, mapping1: ?(number[])], args2: [source2: string, mapping2: ?(number[])], expectedEquals: boolean][]} */ const data = [ [['source1', null], ['source1', null], true], [['source2', null], ['source2', null], true], @@ -76,8 +79,10 @@ function testEquals() { }); } +/** */ function testGetSourceLength() { test('GetSourceLength', () => { + /** @type {[args: [source: string, mapping: number[]], finalLength: number, expectedValue: number][]} */ const data = [ [['source', [1, 1, 1, 1, 1, 1]], 1, 1], [['source', [1, 1, 1, 1, 1, 1]], 2, 2], @@ -103,10 +108,12 @@ function testGetSourceLength() { }); } +/** */ function testCombineInsert() { test('CombineInsert', () => { + /** @type {[args: [source: string, mapping: ?(number[])], expectedArgs: [expectedSource: string, expectedMapping: ?(number[])], operations: [operation: string, arg1: number, arg2: number][]][]} */ const data = [ - // No operations + // No operations [ ['source', null], ['source', [1, 1, 1, 1, 1, 1]], @@ -226,6 +233,7 @@ function testCombineInsert() { } +/** */ function main() { testSource(); testEquals(); diff --git a/test/translator.test.js b/test/translator.test.js index 7a827d39..3db560a7 100644 --- a/test/translator.test.js +++ b/test/translator.test.js @@ -28,6 +28,7 @@ vi.stubGlobal('IDBKeyRange', IDBKeyRange); const dirname = path.dirname(fileURLToPath(import.meta.url)); +/** */ async function main() { const translatorVM = new TranslatorVM(); const dictionaryDirectory = path.join(dirname, 'data', 'dictionaries', 'valid-dictionary1'); @@ -53,6 +54,7 @@ async function main() { case 'findTerms': { const {name, mode, text} = t; + /** @type {import('translation').FindTermsOptions} */ const options = translatorVM.buildOptions(optionsPresets, t.options); const {dictionaryEntries, originalTextLength} = structuredClone(await translatorVM.translator.findTerms(mode, text, options)); const noteDataList = mode !== 'simple' ? structuredClone(dictionaryEntries.map((dictionaryEntry) => translatorVM.createTestAnkiNoteData(structuredClone(dictionaryEntry), mode))) : null; @@ -66,9 +68,10 @@ async function main() { case 'findKanji': { const {name, text} = t; + /** @type {import('translation').FindKanjiOptions} */ const options = translatorVM.buildOptions(optionsPresets, t.options); const dictionaryEntries = structuredClone(await translatorVM.translator.findKanji(text, options)); - const noteDataList = structuredClone(dictionaryEntries.map((dictionaryEntry) => translatorVM.createTestAnkiNoteData(structuredClone(dictionaryEntry), null))); + const noteDataList = structuredClone(dictionaryEntries.map((dictionaryEntry) => translatorVM.createTestAnkiNoteData(structuredClone(dictionaryEntry), 'split'))); actualResults1.push({name, dictionaryEntries}); actualResults2.push({name, noteDataList}); expect(dictionaryEntries).toStrictEqual(expected1.dictionaryEntries); -- cgit v1.2.3