Pop-up dictionary browser extension for language learning. Successor to Yomichan. (PERSONAL FORK)
1/*
2 * Copyright (C) 2024-2025 Yomitan Authors
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18import Ajv from 'ajv';
19import standaloneCode from 'ajv/dist/standalone/index.js';
20import {readFileSync, readdirSync} from 'fs';
21import {dirname, join} from 'path';
22import {fileURLToPath} from 'url';
23import {describe, test} from 'vitest';
24import {parseJson} from '../dev/json.js';
25
26/**
27 * @param {string} path
28 * @returns {import('ajv').AnySchema}
29 */
30function loadSchema(path) {
31 return parseJson(readFileSync(path, {encoding: 'utf8'}));
32}
33
34const extDir = join(dirname(fileURLToPath(import.meta.url)), '..', 'ext');
35
36describe('Ajv schema compilation', () => {
37 const schemaDir = join(extDir, 'data/schemas/');
38 const schemaFileNames = readdirSync(schemaDir);
39 /** @type {{name: string, schema: import('ajv').AnySchema}[]} */
40 const schemaTestCases = [];
41 for (const schemaFileName of schemaFileNames) {
42 schemaTestCases.push({name: schemaFileName, schema: loadSchema(join(schemaDir, schemaFileName))});
43 }
44
45 describe.each(schemaTestCases)('Validating $name', ({schema}) => {
46 test('Compiles without warnings', ({expect}) => {
47 /** @type {string[]} */
48 const messages = [];
49 /**
50 * @param {...unknown} args
51 */
52 const log = (...args) => {
53 messages.push(args.join(' '));
54 };
55 const ajv = new Ajv({
56 schemas: [schema],
57 code: {source: true, esm: true},
58 allowUnionTypes: true,
59 logger: {
60 log,
61 warn: log,
62 error: log,
63 },
64 });
65 standaloneCode(ajv);
66 if (messages.length > 0) {
67 expect.fail(messages.join('\n'));
68 }
69 });
70 });
71});