2026-07-04 20:00:51 +04:00
|
|
|
import {describe, it, expect, vi, afterEach} from 'vitest';
|
2026-07-04 09:47:26 +04:00
|
|
|
|
2026-07-04 20:00:51 +04:00
|
|
|
const {default: createService} = await import('../parole.js');
|
2026-07-04 09:47:26 +04:00
|
|
|
|
|
|
|
|
function fakeDeeplResponse(text) {
|
|
|
|
|
return {
|
|
|
|
|
ok: true,
|
|
|
|
|
json: async () => ({translations: [{text}]})
|
2026-07-04 20:00:51 +04:00
|
|
|
};
|
2026-07-04 09:47:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
describe('Translator (DeepL)', () => {
|
2026-07-04 20:00:51 +04:00
|
|
|
const originalFetch = global.fetch;
|
2026-07-04 09:47:26 +04:00
|
|
|
|
|
|
|
|
afterEach(() => {
|
2026-07-04 20:00:51 +04:00
|
|
|
global.fetch = originalFetch;
|
|
|
|
|
vi.restoreAllMocks();
|
|
|
|
|
});
|
2026-07-04 09:47:26 +04:00
|
|
|
|
|
|
|
|
it('attache un timeout à la requête DeepL', async () => {
|
2026-07-04 20:00:51 +04:00
|
|
|
global.fetch = vi.fn(async () => fakeDeeplResponse('hello'));
|
2026-07-04 09:47:26 +04:00
|
|
|
|
2026-07-04 20:00:51 +04:00
|
|
|
const strapi = {contentType: vi.fn(() => ({uid: 'api::parole.parole', kind: 'collectionType'}))};
|
|
|
|
|
const service = createService({strapi});
|
|
|
|
|
await service.translate('FR', 'EN', 'bonjour');
|
2026-07-04 09:47:26 +04:00
|
|
|
|
2026-07-04 20:00:51 +04:00
|
|
|
const [, options] = global.fetch.mock.calls[0];
|
|
|
|
|
expect(options.signal).toBeInstanceOf(AbortSignal);
|
|
|
|
|
});
|
|
|
|
|
});
|
2026-07-04 09:47:26 +04:00
|
|
|
|
|
|
|
|
describe('translateLyrics', () => {
|
2026-07-04 20:00:51 +04:00
|
|
|
const originalFetch = global.fetch;
|
2026-07-04 09:47:26 +04:00
|
|
|
|
|
|
|
|
afterEach(() => {
|
2026-07-04 20:00:51 +04:00
|
|
|
global.fetch = originalFetch;
|
|
|
|
|
vi.restoreAllMocks();
|
|
|
|
|
});
|
2026-07-04 09:47:26 +04:00
|
|
|
|
|
|
|
|
it('continue les autres langues quand une traduction DeepL échoue', async () => {
|
|
|
|
|
global.fetch = vi.fn(async (_url, options) => {
|
2026-07-04 20:00:51 +04:00
|
|
|
const {target_lang: target} = JSON.parse(options.body);
|
2026-07-04 09:47:26 +04:00
|
|
|
if (target === 'ES') {
|
2026-07-04 20:00:51 +04:00
|
|
|
return {ok: false, status: 500, text: async () => 'boom'};
|
2026-07-04 09:47:26 +04:00
|
|
|
}
|
|
|
|
|
|
2026-07-04 20:00:51 +04:00
|
|
|
return fakeDeeplResponse(`traduit-${target}`);
|
|
|
|
|
});
|
2026-07-04 09:47:26 +04:00
|
|
|
|
|
|
|
|
const strapi = {
|
|
|
|
|
contentType: vi.fn(() => ({uid: 'api::parole.parole', kind: 'collectionType'})),
|
|
|
|
|
log: {error: vi.fn()}
|
2026-07-04 20:00:51 +04:00
|
|
|
};
|
|
|
|
|
const service = createService({strapi});
|
|
|
|
|
const result = await service.translateLyrics('Bonjour le monde');
|
|
|
|
|
|
|
|
|
|
expect(result.anglais).toContain('traduit-EN');
|
|
|
|
|
expect(result.espagnol).toBeUndefined();
|
|
|
|
|
});
|
2026-07-06 00:05:33 +04:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
});
|
2026-07-04 20:00:51 +04:00
|
|
|
});
|