Skip to content

Commit

Permalink
Allow dictionaries to specify minimum compatible Rikaitan version
Browse files Browse the repository at this point in the history
* Add `minimumRikaitanVersion` parameter to dictionary index schema

* Can't use `chrome.runtime` in `DictionaryImporter` class

* Add `rikaitanVersion` parameter to test data

* More test data adjustments

* Use single quotes to please the tests

---------

Co-authored-by: stephenmk <[email protected]>

<rikaitan.link>NzBkZTBmMzFiMjU0Mjk2MjdkYzNlZDYxZTliMThjMjk5MjI5OTZjYQo=</rikaitan.link>
  • Loading branch information
jason-ojisan committed Jan 22, 2025
1 parent eb76794 commit 374a2b8
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 7 deletions.
4 changes: 4 additions & 0 deletions ext/data/schemas/dictionary-index-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
"type": "string",
"description": "Revision of the dictionary. This value is displayed, and used to check for dictionary updates."
},
"minimumRikaitanVersion": {
"type": "string",
"description": "Minimum version of Rikaitan that is compatible with this dictionary."
},
"sequenced": {
"type": "boolean",
"default": false,
Expand Down
14 changes: 12 additions & 2 deletions ext/js/dictionary/dictionary-importer.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
ZipReader as ZipReader0,
configure,
} from '../../lib/zip.js';
import {compareRevisions} from './dictionary-data-util.js';
import {ExtensionError} from '../core/extension-error.js';
import {parseJson} from '../core/json.js';
import {toError} from '../core/to-error.js';
Expand Down Expand Up @@ -180,8 +181,9 @@ export class DictionaryImporter {
}
}

const rikaitanVersion = details.rikaitanVersion;
/** @type {import('dictionary-importer').SummaryDetails} */
const summaryDetails = {prefixWildcardsSupported, counts, styles};
const summaryDetails = {prefixWildcardsSupported, counts, styles, rikaitanVersion};

const summary = this._createSummary(dictionaryTitle, version, index, summaryDetails);
await dictionaryDatabase.bulkAdd('dictionaries', [summary], 0, 1);
Expand Down Expand Up @@ -328,7 +330,15 @@ export class DictionaryImporter {
styles,
};

const {author, url, description, attribution, frequencyMode, isUpdatable, sourceLanguage, targetLanguage} = index;
const {minimumRikaitanVersion, author, url, description, attribution, frequencyMode, isUpdatable, sourceLanguage, targetLanguage} = index;
if (typeof minimumRikaitanVersion === 'string') {
if (details.rikaitanVersion === '0.0.0.0') {
// Running a development version of Rikaitan
} else if (compareRevisions(details.rikaitanVersion, minimumRikaitanVersion)) {
throw new Error(`Dictionary is incompatible with this version of Rikaitan (${details.rikaitanVersion}; minimum required: ${minimumRikaitanVersion})`);
}
summary.minimumRikaitanVersion = minimumRikaitanVersion;
}
if (typeof author === 'string') { summary.author = author; }
if (typeof url === 'string') { summary.url = url; }
if (typeof description === 'string') { summary.description = description; }
Expand Down
1 change: 1 addition & 0 deletions ext/js/pages/settings/dictionary-import-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ export class DictionaryImportController {
const optionsFull = await this._settingsController.getOptionsFull();
const importDetails = {
prefixWildcardsSupported: optionsFull.global.database.prefixWildcardsSupported,
rikaitanVersion: chrome.runtime.getManifest().version,
};

for (let i = 0; i < importProgressTracker.dictionaryCount; ++i) {
Expand Down
8 changes: 4 additions & 4 deletions test/database.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ describe('Database', () => {
// Setup database
const dictionaryDatabase = new DictionaryDatabase();
/** @type {import('dictionary-importer').ImportDetails} */
const defaultImportDetails = {prefixWildcardsSupported: false};
const defaultImportDetails = {prefixWildcardsSupported: false, rikaitanVersion: '0.0.0.0'};

// Database not open
await expect.soft(dictionaryDatabase.deleteDictionary(title, 1000, () => {})).rejects.toThrow('Database not open');
Expand Down Expand Up @@ -167,7 +167,7 @@ describe('Database', () => {
const testDictionarySource = await createTestDictionaryArchiveData(name);

/** @type {import('dictionary-importer').ImportDetails} */
const detaultImportDetails = {prefixWildcardsSupported: false};
const detaultImportDetails = {prefixWildcardsSupported: false, rikaitanVersion: '0.0.0.0'};
await expect.soft(createDictionaryImporter(expect).importDictionary(dictionaryDatabase, testDictionarySource, detaultImportDetails)).rejects.toThrow('Dictionary has invalid data');
await dictionaryDatabase.close();
});
Expand Down Expand Up @@ -199,7 +199,7 @@ describe('Database', () => {
const {result: importDictionaryResult, errors: importDictionaryErrors} = await dictionaryImporter.importDictionary(
dictionaryDatabase,
testDictionarySource,
{prefixWildcardsSupported: true},
{prefixWildcardsSupported: true, rikaitanVersion: '0.0.0.0'},
);

if (importDictionaryResult) {
Expand Down Expand Up @@ -324,7 +324,7 @@ describe('Database', () => {

// Import data
const dictionaryImporter = createDictionaryImporter(expect);
await dictionaryImporter.importDictionary(dictionaryDatabase, testDictionarySource, {prefixWildcardsSupported: true});
await dictionaryImporter.importDictionary(dictionaryDatabase, testDictionarySource, {prefixWildcardsSupported: true, rikaitanVersion: '0.0.0.0'});

// Clear
switch (clearMethod) {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/translator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export async function createTranslatorContext(dictionaryDirectory, dictionaryNam
const {errors, result} = await dictionaryImporter.importDictionary(
dictionaryDatabase,
testDictionaryData,
{prefixWildcardsSupported: true},
{prefixWildcardsSupported: true, rikaitanVersion: '0.0.0.0'},
);

expect(errors.length).toEqual(0);
Expand Down
1 change: 1 addition & 0 deletions types/ext/dictionary-data.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export type Index = {
version?: IndexVersion;
title: string;
revision: string;
minimumRikaitanVersion?: string;
sequenced?: boolean;
isUpdatable?: true;
indexUrl?: string;
Expand Down
3 changes: 3 additions & 0 deletions types/ext/dictionary-importer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ export type ImportResult = {

export type ImportDetails = {
prefixWildcardsSupported: boolean;
rikaitanVersion: string;
};

export type Summary = {
title: string;
revision: string;
sequenced: boolean;
minimumRikaitanVersion?: string;
version: number;
importDate: number;
prefixWildcardsSupported: boolean;
Expand All @@ -67,6 +69,7 @@ export type SummaryDetails = {
prefixWildcardsSupported: boolean;
counts: SummaryCounts;
styles: string;
rikaitanVersion: string;
};

export type SummaryCounts = {
Expand Down

0 comments on commit 374a2b8

Please sign in to comment.