aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2024-02-27 07:14:20 -0500
committerGitHub <noreply@github.com>2024-02-27 12:14:20 +0000
commitc4fea2251606fb0b3d0d761a544dac578f3d9048 (patch)
tree3dbd85877f0d7b3d9de78ace7809e809a7a212d4 /test
parent2e9ea19207a7410f929bb908759d48cb2340f29c (diff)
Update to zip.js for tests (#698)
* Use zip.js * Set compression level to 0 * Update comments
Diffstat (limited to 'test')
-rw-r--r--test/database.test.js29
-rw-r--r--test/dictionary-data-validate.test.js15
-rw-r--r--test/fixtures/translator-test.js7
-rw-r--r--test/playwright/integration.spec.js7
4 files changed, 25 insertions, 33 deletions
diff --git a/test/database.test.js b/test/database.test.js
index 5dbc1040..a930d68d 100644
--- a/test/database.test.js
+++ b/test/database.test.js
@@ -21,8 +21,8 @@ import {readFileSync} from 'node:fs';
import {fileURLToPath} from 'node:url';
import {join, dirname as pathDirname} from 'path';
import {beforeEach, describe, test, vi} from 'vitest';
+import {createDictionaryArchiveData, getDictionaryArchiveIndex} from '../dev/dictionary-archive-util.js';
import {parseJson} from '../dev/json.js';
-import {createDictionaryArchive} from '../dev/util.js';
import {DictionaryDatabase} from '../ext/js/dictionary/dictionary-database.js';
import {DictionaryImporter} from '../ext/js/dictionary/dictionary-importer.js';
import {DictionaryImporterMediaLoader} from './mocks/dictionary-importer-media-loader.js';
@@ -34,11 +34,11 @@ vi.stubGlobal('IDBKeyRange', IDBKeyRange);
/**
* @param {string} dictionary
* @param {string} [dictionaryName]
- * @returns {import('jszip')}
+ * @returns {Promise<ArrayBuffer>}
*/
-function createTestDictionaryArchive(dictionary, dictionaryName) {
+async function createTestDictionaryArchiveData(dictionary, dictionaryName) {
const dictionaryDirectory = join(dirname, 'data', 'dictionaries', dictionary);
- return createDictionaryArchive(dictionaryDirectory, dictionaryName);
+ return await createDictionaryArchiveData(dictionaryDirectory, dictionaryName);
}
/**
@@ -110,10 +110,8 @@ describe('Database', () => {
});
test('Database invalid usage', async ({expect}) => {
// Load dictionary data
- const testDictionary = createTestDictionaryArchive('valid-dictionary1');
- const testDictionarySource = await testDictionary.generateAsync({type: 'arraybuffer'});
- /** @type {import('dictionary-data').Index} */
- const testDictionaryIndex = parseJson(await testDictionary.files['index.json'].async('string'));
+ const testDictionarySource = await createTestDictionaryArchiveData('valid-dictionary1');
+ const testDictionaryIndex = await getDictionaryArchiveIndex(testDictionarySource);
const title = testDictionaryIndex.title;
const titles = new Map([
@@ -165,8 +163,7 @@ describe('Database', () => {
const dictionaryDatabase = new DictionaryDatabase();
await dictionaryDatabase.prepare();
- const testDictionary = createTestDictionaryArchive(name);
- const testDictionarySource = await testDictionary.generateAsync({type: 'arraybuffer'});
+ const testDictionarySource = await createTestDictionaryArchiveData(name);
/** @type {import('dictionary-importer').ImportDetails} */
const detaultImportDetails = {prefixWildcardsSupported: false};
@@ -183,10 +180,8 @@ describe('Database', () => {
const fakeImportDate = testData.expectedSummary.importDate;
// Load dictionary data
- const testDictionary = createTestDictionaryArchive('valid-dictionary1');
- const testDictionarySource = await testDictionary.generateAsync({type: 'arraybuffer'});
- /** @type {import('dictionary-data').Index} */
- const testDictionaryIndex = parseJson(await testDictionary.files['index.json'].async('string'));
+ const testDictionarySource = await createTestDictionaryArchiveData('valid-dictionary1');
+ const testDictionaryIndex = await getDictionaryArchiveIndex(testDictionarySource);
const title = testDictionaryIndex.title;
const titles = new Map([
@@ -315,10 +310,8 @@ describe('Database', () => {
describe.each(cleanupTestCases)('Testing cleanup method $clearMethod', ({clearMethod}) => {
test('Import data and test', async ({expect}) => {
// Load dictionary data
- const testDictionary = createTestDictionaryArchive('valid-dictionary1');
- const testDictionarySource = await testDictionary.generateAsync({type: 'arraybuffer'});
- /** @type {import('dictionary-data').Index} */
- const testDictionaryIndex = parseJson(await testDictionary.files['index.json'].async('string'));
+ const testDictionarySource = await createTestDictionaryArchiveData('valid-dictionary1');
+ const testDictionaryIndex = await getDictionaryArchiveIndex(testDictionarySource);
// Setup database
const dictionaryDatabase = new DictionaryDatabase();
diff --git a/test/dictionary-data-validate.test.js b/test/dictionary-data-validate.test.js
index b09e4ae0..a8d9f1ab 100644
--- a/test/dictionary-data-validate.test.js
+++ b/test/dictionary-data-validate.test.js
@@ -19,19 +19,19 @@
import {fileURLToPath} from 'node:url';
import path from 'path';
import {describe, it} from 'vitest';
+import {createDictionaryArchiveData} from '../dev/dictionary-archive-util.js';
import * as dictionaryValidate from '../dev/dictionary-validate.js';
-import {createDictionaryArchive} from '../dev/util.js';
const dirname = path.dirname(fileURLToPath(import.meta.url));
/**
* @param {string} dictionary
* @param {string} [dictionaryName]
- * @returns {import('jszip')}
+ * @returns {Promise<ArrayBuffer>}
*/
-function createTestDictionaryArchive(dictionary, dictionaryName) {
+async function createTestDictionaryArchiveData(dictionary, dictionaryName) {
const dictionaryDirectory = path.join(dirname, 'data', 'dictionaries', dictionary);
- return createDictionaryArchive(dictionaryDirectory, dictionaryName);
+ return await createDictionaryArchiveData(dictionaryDirectory, dictionaryName);
}
describe('Dictionary validation', () => {
@@ -47,11 +47,12 @@ describe('Dictionary validation', () => {
const schemas = dictionaryValidate.getSchemas();
describe.each(testCases)('Test dictionary $name', ({name, valid}) => {
it(`should be ${valid ? 'valid' : 'invalid'}`, async ({expect}) => {
- const archive = createTestDictionaryArchive(name);
+ const archive = await createTestDictionaryArchiveData(name);
+ const promise = dictionaryValidate.validateDictionary(null, archive, schemas);
await (
valid ?
- expect(dictionaryValidate.validateDictionary(null, archive, schemas)).resolves.not.toThrow() :
- expect(dictionaryValidate.validateDictionary(null, archive, schemas)).rejects.toThrow()
+ expect(promise).resolves.not.toThrow() :
+ expect(promise).rejects.toThrow()
);
});
});
diff --git a/test/fixtures/translator-test.js b/test/fixtures/translator-test.js
index 95218830..ff3782a7 100644
--- a/test/fixtures/translator-test.js
+++ b/test/fixtures/translator-test.js
@@ -21,8 +21,8 @@ import {readFileSync} from 'fs';
import {fileURLToPath} from 'node:url';
import {dirname, join} from 'path';
import {expect, vi} from 'vitest';
+import {createDictionaryArchiveData} from '../../dev/dictionary-archive-util.js';
import {parseJson} from '../../dev/json.js';
-import {createDictionaryArchive} from '../../dev/util.js';
import {DictionaryDatabase} from '../../ext/js/dictionary/dictionary-database.js';
import {DictionaryImporter} from '../../ext/js/dictionary/dictionary-importer.js';
import {Translator} from '../../ext/js/language/translator.js';
@@ -45,8 +45,7 @@ vi.stubGlobal('chrome', chrome);
*/
export async function createTranslatorContext(dictionaryDirectory, dictionaryName) {
// Dictionary
- const testDictionary = createDictionaryArchive(dictionaryDirectory, dictionaryName);
- const testDictionaryContent = await testDictionary.generateAsync({type: 'arraybuffer'});
+ const testDictionaryData = await createDictionaryArchiveData(dictionaryDirectory, dictionaryName);
// Setup database
const dictionaryImporterMediaLoader = new DictionaryImporterMediaLoader();
@@ -56,7 +55,7 @@ export async function createTranslatorContext(dictionaryDirectory, dictionaryNam
const {errors} = await dictionaryImporter.importDictionary(
dictionaryDatabase,
- testDictionaryContent,
+ testDictionaryData,
{prefixWildcardsSupported: true}
);
diff --git a/test/playwright/integration.spec.js b/test/playwright/integration.spec.js
index 8e641397..bdbe80e3 100644
--- a/test/playwright/integration.spec.js
+++ b/test/playwright/integration.spec.js
@@ -16,7 +16,7 @@
*/
import path from 'path';
-import {createDictionaryArchive} from '../../dev/util.js';
+import {createDictionaryArchiveData} from '../../dev/dictionary-archive-util.js';
import {deferPromise} from '../../ext/js/core/utilities.js';
import {
expect,
@@ -67,12 +67,11 @@ test('anki add', async ({context, page, extensionId}) => {
await page.goto(`chrome-extension://${extensionId}/settings.html`);
// Load in test dictionary
- const dictionary = createDictionaryArchive(path.join(root, 'test/data/dictionaries/valid-dictionary1'), 'valid-dictionary1');
- const testDictionarySource = await dictionary.generateAsync({type: 'arraybuffer'});
+ const dictionary = await createDictionaryArchiveData(path.join(root, 'test/data/dictionaries/valid-dictionary1'), 'valid-dictionary1');
await page.locator('input[id="dictionary-import-file-input"]').setInputFiles({
name: 'valid-dictionary1.zip',
mimeType: 'application/x-zip',
- buffer: Buffer.from(testDictionarySource)
+ buffer: Buffer.from(dictionary)
});
await expect(page.locator('id=dictionaries')).toHaveText('Dictionaries (1 installed, 1 enabled)', {timeout: 5 * 60 * 1000});