Files
api.pawol.nu/src/api/parole/services/__tests__/parole.test.js
T
cedric b01132f104
Déploiement API BETA / build (push) Successful in 2m26s
Déploiement API BETA / deploy (push) Successful in 47s
Vérification PR / build (pull_request) Successful in 2m21s
Vérification PR / deploy-beta (pull_request) Successful in 43s
feat: ajouter 14 langues africaines (dont le yoruba, hors DeepL)
2026-07-06 00:05:33 +04:00

85 lines
2.7 KiB
JavaScript

import {describe, it, expect, vi, afterEach} from 'vitest';
const {default: createService} = await import('../parole.js');
function fakeDeeplResponse(text) {
return {
ok: true,
json: async () => ({translations: [{text}]})
};
}
describe('Translator (DeepL)', () => {
const originalFetch = global.fetch;
afterEach(() => {
global.fetch = originalFetch;
vi.restoreAllMocks();
});
it('attache un timeout à la requête DeepL', async () => {
global.fetch = vi.fn(async () => fakeDeeplResponse('hello'));
const strapi = {contentType: vi.fn(() => ({uid: 'api::parole.parole', kind: 'collectionType'}))};
const service = createService({strapi});
await service.translate('FR', 'EN', 'bonjour');
const [, options] = global.fetch.mock.calls[0];
expect(options.signal).toBeInstanceOf(AbortSignal);
});
});
describe('translateLyrics', () => {
const originalFetch = global.fetch;
afterEach(() => {
global.fetch = originalFetch;
vi.restoreAllMocks();
});
it('continue les autres langues quand une traduction DeepL échoue', async () => {
global.fetch = vi.fn(async (_url, options) => {
const {target_lang: target} = JSON.parse(options.body);
if (target === 'ES') {
return {ok: false, status: 500, text: async () => 'boom'};
}
return fakeDeeplResponse(`traduit-${target}`);
});
const strapi = {
contentType: vi.fn(() => ({uid: 'api::parole.parole', kind: 'collectionType'})),
log: {error: vi.fn()}
};
const service = createService({strapi});
const result = await service.translateLyrics('Bonjour le monde');
expect(result.anglais).toContain('traduit-EN');
expect(result.espagnol).toBeUndefined();
});
it('traduit les langues africaines ajoutées, sans appeler DeepL pour le yoruba', async () => {
global.fetch = vi.fn(async (_url, options) => {
const {target_lang: target} = JSON.parse(options.body);
return fakeDeeplResponse(`traduit-${target}`);
});
const strapi = {
contentType: vi.fn(() => ({uid: 'api::parole.parole', kind: 'collectionType'})),
log: {error: vi.fn()}
};
const service = createService({strapi});
const result = await service.translateLyrics('Bonjour le monde');
expect(result.swahili).toContain('traduit-SW');
expect(result.lingala).toContain('traduit-LN');
expect(result.wolof).toContain('traduit-WO');
expect(result.hausa).toContain('traduit-HA');
expect(result.yoruba).toBeUndefined();
const calledTargets = global.fetch.mock.calls.map(([, options]) => JSON.parse(options.body).target_lang);
expect(calledTargets).not.toContain('YO');
expect(calledTargets).not.toContain(undefined);
});
});