76 lines
2.5 KiB
TypeScript
76 lines
2.5 KiB
TypeScript
import { describe, expect, it, vi } from 'vitest';
|
|
import {
|
|
clearStaleBasqueTokenizers,
|
|
isBasqueTokenizerUrl,
|
|
TRANSFORMERS_CACHE_NAME,
|
|
} from './translationModelCache';
|
|
|
|
const EU_TOKENIZER =
|
|
'https://relay.skate-spots.org/models/Xenova/opus-mt-eu-en/resolve/main/tokenizer.json';
|
|
const EN_TOKENIZER =
|
|
'https://relay.skate-spots.org/models/Xenova/opus-mt-en-eu/resolve/main/tokenizer.json';
|
|
|
|
describe('translationModelCache', () => {
|
|
it('matches both Basque tokenizer URLs only', () => {
|
|
expect(isBasqueTokenizerUrl(EU_TOKENIZER)).toBe(true);
|
|
expect(isBasqueTokenizerUrl(EN_TOKENIZER)).toBe(true);
|
|
expect(
|
|
isBasqueTokenizerUrl(
|
|
'https://relay.skate-spots.org/models/Xenova/opus-mt-eu-en/resolve/main/config.json',
|
|
),
|
|
).toBe(false);
|
|
expect(
|
|
isBasqueTokenizerUrl(
|
|
'https://relay.skate-spots.org/models/Xenova/opus-mt-eu-en/resolve/main/onnx/encoder_model_quantized.onnx',
|
|
),
|
|
).toBe(false);
|
|
expect(
|
|
isBasqueTokenizerUrl(
|
|
'https://relay.skate-spots.org/models/Xenova/m2m100_418M/resolve/main/tokenizer.json',
|
|
),
|
|
).toBe(false);
|
|
expect(isBasqueTokenizerUrl('not a url')).toBe(false);
|
|
});
|
|
|
|
it('deletes only the cached Basque tokenizer entries', async () => {
|
|
const deleted: string[] = [];
|
|
const requests = [
|
|
{ url: EU_TOKENIZER },
|
|
{ url: EN_TOKENIZER },
|
|
{
|
|
url: 'https://relay.skate-spots.org/models/Xenova/opus-mt-eu-en/resolve/main/config.json',
|
|
},
|
|
{ url: 'https://relay.skate-spots.org/models/Xenova/m2m100_418M/resolve/main/tokenizer.json' },
|
|
];
|
|
const open = vi.fn(async (name: string) => {
|
|
expect(name).toBe(TRANSFORMERS_CACHE_NAME);
|
|
return {
|
|
keys: async () => requests,
|
|
delete: async (request: { url: string }) => {
|
|
deleted.push(request.url);
|
|
return true;
|
|
},
|
|
};
|
|
});
|
|
|
|
const removed = await clearStaleBasqueTokenizers(cacheStorageStub(open));
|
|
|
|
expect(removed).toBe(2);
|
|
expect(deleted).toEqual([EU_TOKENIZER, EN_TOKENIZER]);
|
|
});
|
|
|
|
it('returns 0 when Cache Storage is unavailable', async () => {
|
|
await expect(clearStaleBasqueTokenizers(undefined)).resolves.toBe(0);
|
|
});
|
|
|
|
it('returns 0 when opening the cache fails', async () => {
|
|
const open = vi.fn(async () => {
|
|
throw new Error('blocked');
|
|
});
|
|
await expect(clearStaleBasqueTokenizers(cacheStorageStub(open))).resolves.toBe(0);
|
|
});
|
|
});
|
|
|
|
function cacheStorageStub(open: (name: string) => Promise<unknown>) {
|
|
return { open } as unknown as Pick<CacheStorage, 'open'>;
|
|
}
|