Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mololab
GitHub Repository: mololab/json-translator
Path: blob/master/src/test/translator.spec.ts
235 views
1
/// <reference types="../global" />
2
import 'openai/shims/node';
3
import nock from 'nock';
4
import { plaintranslate } from '../core/translator';
5
import { DeepLTranslateLanguages } from '../modules/languages';
6
import { TranslationConfig, TranslationModules } from '../modules/modules';
7
import { default_concurrency_limit, default_fallback } from '../utils/micro';
8
9
describe('TRANSLATOR', () => {
10
it('should translate using DeepL', async function() {
11
process.env.DEEPL_API_KEY = 'deepl_api_key';
12
13
const scope = nock('https://api-free.deepl.com', {
14
reqheaders: {
15
authorization: `DeepL-Auth-Key ${process.env.DEEPL_API_KEY}`,
16
'content-type': 'application/json',
17
},
18
})
19
.post('/v2/translate', {
20
text: ['hello'],
21
target_lang: DeepLTranslateLanguages.German,
22
source_lang: DeepLTranslateLanguages.English,
23
})
24
.reply(200, {
25
translations: [
26
{
27
detected_source_language: 'EN',
28
text: 'hallo',
29
},
30
],
31
});
32
33
let config: TranslationConfig = {
34
moduleKey: 'deepl',
35
TranslationModule: TranslationModules['deepl'],
36
concurrencyLimit: default_concurrency_limit,
37
fallback: default_fallback,
38
cacheEnabled: false,
39
};
40
41
const result = await plaintranslate(
42
config,
43
'hello',
44
DeepLTranslateLanguages.English,
45
DeepLTranslateLanguages.German,
46
[]
47
);
48
expect(result).toEqual('hallo');
49
scope.done();
50
});
51
});
52
53