From 8b943cc97fab890085448122e7c13dd035d0e238 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Wed, 20 Dec 2023 00:47:15 -0500 Subject: JSON validation (#394) * Set up JSON testing * Add schema validation * Use parseJson * Finish types * Disambiguate ext/json-schema from node dependency with the same name * Add support for specifying the jsconfig file * Don't expose types * Update types * Use dictionary map type * Fix types * Fix AJV warnings * Move types * Move anb rename file * Move common mocks * Simplify types --- types/test/json.d.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ types/test/mocks.d.ts | 32 ++++++++++++++++++++++++++++++++ types/test/translator.d.ts | 45 +++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 115 insertions(+), 4 deletions(-) create mode 100644 types/test/json.d.ts create mode 100644 types/test/mocks.d.ts (limited to 'types/test') diff --git a/types/test/json.d.ts b/types/test/json.d.ts new file mode 100644 index 00000000..9ae21b0f --- /dev/null +++ b/types/test/json.d.ts @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2023 Yomitan 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 . + */ + +import type {Schema} from 'ajv'; + +export type JsonInfo = { + files: JsonFileInfo[]; +}; + +export type JsonFileInfo = JsonFileIgnoreInfo | JsonFileParseInfo; + +export type JsonFileIgnoreInfo = { + path: string; + ignore: true; +}; + +export type JsonFileParseInfo = { + path: string; + ignore?: undefined; + typeFile: string; + type: string; + schema?: string; + jsconfig?: JsconfigType; +}; + +export type AjvSchema = Schema; + +export type JsconfigType = 'main' | 'dev' | 'test'; diff --git a/types/test/mocks.d.ts b/types/test/mocks.d.ts new file mode 100644 index 00000000..13b56ac6 --- /dev/null +++ b/types/test/mocks.d.ts @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2023 Yomitan 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 . + */ + +export type ChromeMock = { + runtime: { + getURL(path: string): string; + }; +}; + +export type FetchMock = (url: string) => Promise; + +export type FetchResponseMock = { + ok: boolean; + status: number; + statusText: string; + text(): Promise; + json(): Promise; +}; diff --git a/types/test/translator.d.ts b/types/test/translator.d.ts index 3e4c8b9d..7dff116e 100644 --- a/types/test/translator.d.ts +++ b/types/test/translator.d.ts @@ -15,12 +15,49 @@ * along with this program. If not, see . */ -import type {OptionsPresetObject} from 'dev/vm'; +import type {FindTermsMatchType, FindTermsSortOrder, FindTermsVariantMode, FindTermsEmphaticSequencesMode, FindKanjiDictionary, FindTermDictionary} from '../ext/translation'; import type {FindTermsMode} from 'translator'; import type {DictionaryEntry} from 'dictionary'; import type {NoteData} from 'anki-templates'; import type {NoteFields} from 'anki'; +export type OptionsPresetObject = { + [key: string]: OptionsPreset; +}; + +export type OptionsList = string | (string | OptionsPreset)[]; + +export type OptionsPreset = FindKanjiOptionsPreset | FindTermsOptionsPreset; + +export type FindKanjiOptionsPreset = { + enabledDictionaryMap?: [key: string, value: FindKanjiDictionary][]; + removeNonJapaneseCharacters?: boolean; +}; + +export type FindTermsOptionsPreset = { + matchType?: FindTermsMatchType; + deinflect?: boolean; + mainDictionary?: string; + sortFrequencyDictionary?: string | null; + sortFrequencyDictionaryOrder?: FindTermsSortOrder; + removeNonJapaneseCharacters?: boolean; + convertHalfWidthCharacters?: FindTermsVariantMode; + convertNumericCharacters?: FindTermsVariantMode; + convertAlphabeticCharacters?: FindTermsVariantMode; + convertHiraganaToKatakana?: FindTermsVariantMode; + convertKatakanaToHiragana?: FindTermsVariantMode; + collapseEmphaticSequences?: FindTermsEmphaticSequencesMode; + textReplacements?: (FindTermsTextReplacement[] | null)[]; + enabledDictionaryMap?: [key: string, value: FindTermDictionary][]; + excludeDictionaryDefinitions?: string[] | null; +}; + +export type FindTermsTextReplacement = { + pattern: string; + flags: string; + replacement: string; +}; + export type TranslatorTestInputs = { optionsPresets: OptionsPresetObject; tests: TestInput[]; @@ -32,7 +69,7 @@ export type TestInputFindKanji = { func: 'findKanji'; name: string; text: string; - options: string; + options: OptionsList; }; export type TestInputFindTerm = { @@ -40,7 +77,7 @@ export type TestInputFindTerm = { name: string; mode: FindTermsMode; text: string; - options: string; + options: OptionsList; }; export type TranslatorTestResults = TranslatorTestResult[]; @@ -55,7 +92,7 @@ export type TranslatorTestNoteDataResults = TranslatorTestNoteDataResult[]; export type TranslatorTestNoteDataResult = { name: string; - noteDataList: NoteData[]; + noteDataList: Omit[] | null; }; export type AnkiNoteBuilderTestResults = AnkiNoteBuilderTestResult[]; -- cgit v1.2.3