From 321d26b47093df75fd6b9ee0bbb5dd8194b2f2a1 Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Mon, 7 Oct 2024 14:55:12 -0300 Subject: [PATCH] test(DeepLTranslator): create multiple tests --- src/translators/DeepLTranslator.test.ts | 139 ++++++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 src/translators/DeepLTranslator.test.ts diff --git a/src/translators/DeepLTranslator.test.ts b/src/translators/DeepLTranslator.test.ts new file mode 100644 index 00000000..e261a9b6 --- /dev/null +++ b/src/translators/DeepLTranslator.test.ts @@ -0,0 +1,139 @@ +import { assertEquals } from '@std/assert'; + +import { Conf } from '@/config.ts'; +import { DeepLTranslator } from '@/translators/DeepLTranslator.ts'; +import { getLanguage } from '@/test.ts'; + +const endpoint = Conf.translationProviderEndpoint; +const apiKey = Conf.translationProviderApiKey; +const translationProvider = Conf.translationProvider; +const deepL = 'DeepL'.toLowerCase(); + +Deno.test('Translate status with EMPTY media_attachments and WITHOUT poll', { + ignore: !(translationProvider === deepL && apiKey), +}, async () => { + const translator = new DeepLTranslator({ fetch: fetch, endpoint, apiKey: apiKey as string }); + + const mastodonTranslation = await translator.translate( + 'Bom dia amigos do Element, meu nome é Patrick', + '', + [], + null, + 'pt', + 'en', + ); + + assertEquals(getLanguage(mastodonTranslation.data.content), 'en'); + assertEquals(mastodonTranslation.data.spoiler_text, ''); + assertEquals(mastodonTranslation.data.media_attachments, []); + assertEquals(mastodonTranslation.data.poll, null); + assertEquals(mastodonTranslation.data.provider, 'DeepL.com'); +}); + +Deno.test('Translate status WITH auto detect and with EMPTY media_attachments and WITHOUT poll', { + ignore: !(translationProvider === deepL && apiKey), +}, async () => { + const translator = new DeepLTranslator({ fetch: fetch, endpoint, apiKey: apiKey as string }); + + const mastodonTranslation = await translator.translate( + 'Bom dia amigos do Element, meu nome é Patrick', + '', + [], + null, + undefined, + 'en', + ); + + assertEquals(getLanguage(mastodonTranslation.data.content), 'en'); + assertEquals(mastodonTranslation.data.spoiler_text, ''); + assertEquals(mastodonTranslation.data.media_attachments, []); + assertEquals(mastodonTranslation.data.poll, null); + assertEquals(mastodonTranslation.data.provider, 'DeepL.com'); +}); + +Deno.test('Translate status WITH media_attachments and WITHOUT poll', { + ignore: !(translationProvider === deepL && apiKey), +}, async () => { + const translator = new DeepLTranslator({ fetch: fetch, endpoint, apiKey: apiKey as string }); + + const mastodonTranslation = await translator.translate( + 'Hello my friends, my name is Alex and I am american.', + "That is spoiler isn't it", + [{ id: 'game', description: 'I should be playing Miles Edgeworth with my wife' }], + null, + 'en', + 'pt', + ); + + assertEquals(getLanguage(mastodonTranslation.data.content), 'pt'); + assertEquals(getLanguage(mastodonTranslation.data.spoiler_text), 'pt'); + assertEquals(mastodonTranslation.data.media_attachments.map((value) => getLanguage(value.description)), ['pt']); + assertEquals(mastodonTranslation.data.poll, null); + assertEquals(mastodonTranslation.data.provider, 'DeepL.com'); +}); + +Deno.test('Translate status WITHOUT media_attachments and WITH poll', { + ignore: !(translationProvider === deepL && apiKey), +}, async () => { + const translator = new DeepLTranslator({ fetch: fetch, endpoint, apiKey: apiKey as string }); + + const poll = { + 'id': '34858', + 'options': [ + { + 'title': 'Kill him right now', + }, + { + 'title': 'Save him right now', + }, + ], + }; + + const mastodonTranslation = await translator.translate( + 'Hello my friends, my name is Alex and I am american.', + '', + [], + poll, + 'en', + 'pt', + ); + + assertEquals(getLanguage(mastodonTranslation.data.content), 'pt'); + assertEquals(mastodonTranslation.data.spoiler_text, ''); + assertEquals(mastodonTranslation.data.media_attachments, []); + assertEquals(mastodonTranslation.data.poll?.options.map((value) => getLanguage(value.title)), ['pt', 'pt']); + assertEquals(mastodonTranslation.data.provider, 'DeepL.com'); +}); + +Deno.test('Translate status WITH media_attachments and WITH poll', { + ignore: !(translationProvider === deepL && apiKey), +}, async () => { + const translator = new DeepLTranslator({ fetch: fetch, endpoint, apiKey: apiKey as string }); + + const poll = { + 'id': '34858', + 'options': [ + { + 'title': 'Kill him right now', + }, + { + 'title': 'Save him right now', + }, + ], + }; + + const mastodonTranslation = await translator.translate( + 'Hello my friends, my name is Alex and I am american.', + '', + [{ id: 'game', description: 'I should be playing Miles Edgeworth with my wife' }], + poll, + 'en', + 'pt', + ); + + assertEquals(getLanguage(mastodonTranslation.data.content), 'pt'); + assertEquals(mastodonTranslation.data.spoiler_text, ''); + assertEquals(mastodonTranslation.data.media_attachments.map((value) => getLanguage(value.description)), ['pt']); + assertEquals(mastodonTranslation.data.poll?.options.map((value) => getLanguage(value.title)), ['pt', 'pt']); + assertEquals(mastodonTranslation.data.provider, 'DeepL.com'); +});