Audit sécurité/qualité : corrections critiques, tests, CI et lint #4
@@ -0,0 +1,60 @@
|
||||
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()
|
||||
})
|
||||
})
|
||||
@@ -64,7 +64,8 @@ class Translator {
|
||||
text: Array.isArray(text) ? text : [text],
|
||||
source_lang: origin,
|
||||
target_lang: target,
|
||||
})
|
||||
}),
|
||||
signal: AbortSignal.timeout(15_000)
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
@@ -90,8 +91,12 @@ module.exports = createCoreService('api::parole.parole', ({strapi}) => ({
|
||||
for (const lang of ALL_LANGS) {
|
||||
if (lang === 'fr') continue
|
||||
const { field, deeplTarget, suffix } = LANG_MAP[lang]
|
||||
const translated = await this.translate('FR', deeplTarget, parolesFR)
|
||||
result[field] = translated + suffix
|
||||
try {
|
||||
const translated = await this.translate('FR', deeplTarget, parolesFR)
|
||||
result[field] = translated + suffix
|
||||
} catch (err) {
|
||||
strapi.log.error(`DeepL (${deeplTarget}): ${err.message}`)
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
|
||||
Reference in New Issue
Block a user