UNPKG

asksuite-core

Version:
236 lines (184 loc) 9.23 kB
const fs = require('fs'); const path = require('path'); const TranslatorUtil = require('../util/Translator'); const config = require('../mocks/json/config'); const { removePreventTranslationMarkers } = require('../translator/util'); const MockGenerator = require('../mocks/MockGenerator'); describe('Translator module', () => { const languagesPath = path.join(__dirname, '..', 'mocks/files/languages.json'); const treeFilePath = path.join(__dirname, '..', 'mocks/files/tree.json'); const simpleTreeFilePath = path.join(__dirname, '..', 'mocks/files/simpletree.json'); const treeFileTJOPath = path.join(__dirname, '..', 'mocks/files/tree_TJO.json'); const languages = JSON.parse(fs.readFileSync(languagesPath).toString()); const treeData = JSON.parse(fs.readFileSync(treeFilePath).toString()); const simpleTreeData = JSON.parse(fs.readFileSync(simpleTreeFilePath).toString()); const treeDataTJO = JSON.parse(fs.readFileSync(treeFileTJOPath).toString()); it('Should return translated from translation api ', async () => { const translator = new TranslatorUtil( '', '', 'https://t3thdppkkbmqkd24caunyqqd5a0vvzxx.lambda-url.us-east-1.on.aws'); const translation = await translator.translateByTranslationApi('Hello how are you?', 'en-us', 'pt-br', MockGenerator.getDefaultLanguages()); expect(translation.translation).toEqual('Olá, como você está?'); }, 100000); it('Should return translated from googleTranslateIfNeeded', async () => { const translator = new TranslatorUtil( '', config.GOOGLE_TRANSLATE_KEY, 'https://t3thdppkkbmqkd24caunyqqd5a0vvzxx.lambda-url.us-east-1.on.aws'); const translation = await translator.googleTranslateIfNeeded('Hello how are you?', 'en-us', MockGenerator.getDefaultLanguages(), 'pt-br'); expect(translation.translation).toEqual('Olá, como vai?'); }, 100000); it.skip('Should return translated object equals when use asksuite-translate-json-object module', async () => { const newTranslatedObject = await require('../translator')({ INFOCHAT_DATA_SERVER: 'http://localhost:9000/api', REDIS_URL: 'redis://localhost', GOOGLE_TRANSLATE_KEY: config.GOOGLE_TRANSLATE_KEY, }).translate({ chatTreeId: 'serrabelahospedaria', json: treeData }, 'en', 'pt-br', languages); expect(newTranslatedObject).toEqual(treeDataTJO); }, 100000); it('Should return translated object and keep mustache params', async () => { const newTranslatedObject = await require('../translator')({ INFOCHAT_DATA_SERVER: 'http://localhost:9000/api', REDIS_URL: 'redis://localhost', GOOGLE_TRANSLATE_KEY: config.GOOGLE_TRANSLATE_KEY, }).translate( { chatTreeId: 'serrabelahospedaria', json: simpleTreeData }, 'en', 'pt-br', languages, ); expect(newTranslatedObject.json.text).toEqual( '{{#facebook}}Hello {{leadName}} are you on Facebook{{/facebook}}{{#whatsapp}}Hello {{leadName}} are you on WhatsApp{{/whatsapp}}', ); }, 100000); it('Should return not translate dialog with mustache expression', async () => { const newTranslatedObject = await require('../translator')({ INFOCHAT_DATA_SERVER: 'http://localhost:9000/api', REDIS_URL: 'redis://localhost', GOOGLE_TRANSLATE_KEY: config.GOOGLE_TRANSLATE_KEY, }).translate( { chatTreeId: 'serrabelahospedaria', json: simpleTreeData }, 'en', 'pt-br', languages, ); expect(newTranslatedObject.json.goToDialog).toEqual( '{{#facebook}}serrabelahospedaria.facebook{{/facebook}}{{#whatsapp}}serrabelahospedaria.whatsapp{{/whatsapp}}', ); }, 100000); }); describe('Variable extractor test', () => { it('Should parse single variable text', async () => { const text = '{{variableName}}'; const result = TranslatorUtil.extractVariables(text); expect(result[0]).toEqual('variableName'); }); it('Should parse multiple variable text', async () => { const text = '{{variableName}} {{variableName2}}'; const result = TranslatorUtil.extractVariables(text); expect(result.length).toBe(2); expect(result).toContain('variableName'); expect(result).toContain('variableName2'); }); it('Should parse variables in text', async () => { const text = "Hello my name is {{variableName}} and it's a pleasure to receive you at {{hotelName}}"; const result = TranslatorUtil.extractVariables(text); expect(result.length).toBe(2); expect(result).toContain('variableName'); expect(result).toContain('hotelName'); }); it('Should parse object variable text', async () => { const text = '{{variableName.Child1}}'; const result = TranslatorUtil.extractVariables(text); expect(result[0]).toEqual('variableName.Child1'); }); }); describe('Variable protect test', () => { it('Should protect single variable text', async () => { const text = '{{variableName}}'; const result = TranslatorUtil.protectMustacheVariablesTranslation(text); expect(result).toEqual('{{_v_a_r_i_a_b_l_e_N_a_m_e}}'); }); it('Should protect single variable with space text', async () => { const text = '{{ variableName }}'; const result = TranslatorUtil.protectMustacheVariablesTranslation(text); expect(result).toEqual('{{_ _v_a_r_i_a_b_l_e_N_a_m_e_ }}'); }); it('Should protect multiple variable text', async () => { const text = '{{variableName}} {{variableName2}}'; const result = TranslatorUtil.protectMustacheVariablesTranslation(text); expect(result).toEqual('{{_v_a_r_i_a_b_l_e_N_a_m_e}} {{_v_a_r_i_a_b_l_e_N_a_m_e_2}}'); }); it('Should protect variables in text', async () => { const text = "Hello my name is {{variableName}} and it's a pleasure to receive you at {{hotelName}}"; const result = TranslatorUtil.protectMustacheVariablesTranslation(text); expect(result).toEqual( "Hello my name is {{_v_a_r_i_a_b_l_e_N_a_m_e}} and it's a pleasure to receive you at {{_h_o_t_e_l_N_a_m_e}}", ); }); it('Should protect object variable text', async () => { const text = '{{variableName.Child1}}'; const result = TranslatorUtil.protectMustacheVariablesTranslation(text); expect(result).toEqual('{{_v_a_r_i_a_b_l_e_N_a_m_e_._C_h_i_l_d_1}}'); }); it('Should protect object variable text with underscore', async () => { const text = 'R$ {{{ tour.menor_valor }}}'; const result = TranslatorUtil.protectMustacheVariablesTranslation(text); expect(result).toEqual('R$ {{{_ _t_o_u_r_._m_e_n_o_r___v_a_l_o_r_ }}}'); }); }); describe('Variable unprotect test', () => { it('Should unprotect single variable text', async () => { const text = '{{_v_a_r_i_a_b_l_e_N_a_m_e}}'; const result = TranslatorUtil.unprotectMustacheVariablesTranslation(text); expect(result).toEqual('{{variableName}}'); }); it('Should unprotect single variable with space text', async () => { const text = '{{_ _v_a_r_i_a_b_l_e_N_a_m_e_ }}'; const result = TranslatorUtil.unprotectMustacheVariablesTranslation(text); expect(result).toEqual('{{variableName}}'); }); it('Should unprotect single variable text with underscore', async () => { const text = 'R $ {{{_ _t_o_u_r _._ m_e_n_o_r___v_a_l_o_r_}}}'; const result = TranslatorUtil.unprotectMustacheVariablesTranslation(text); expect(result).toEqual('R $ {{{tour.menor_valor}}}'); }); it('Should unprotect multiple variable text', async () => { const text = '{{_v_a_r_i_a_b_l_e_N_a_m_e}} {{_v_a_r_i_a_b_l_e_N_a_m_e_2}}'; const result = TranslatorUtil.unprotectMustacheVariablesTranslation(text); expect(result).toEqual('{{variableName}} {{variableName2}}'); }); it('Should unprotect variables in text', async () => { const text = "Hello my name is {{_v_a_r_i_a_b_l_e_N_a_m_e}} and it's a pleasure to receive you at {{_h_o_t_e_l_N_a_m_e}}"; const result = TranslatorUtil.unprotectMustacheVariablesTranslation(text); expect(result).toEqual( "Hello my name is {{variableName}} and it's a pleasure to receive you at {{hotelName}}", ); }); it('Should unprotect object variable text', async () => { const text = '{{_v_a_r_i_a_b_l_e_N_a_m_e_._C_h_i_l_d_1}}'; const result = TranslatorUtil.unprotectMustacheVariablesTranslation(text); expect(result).toEqual('{{variableName.Child1}}'); }); it('Should unprotect object variable text complex expression', async () => { const text = '{{#facebook}}Olá {{leadName}} você está no Facebook{{/facebook}}{{#whatsapp}}Olá {{leadName}} você está no WhatsApp{{/whatsapp}}'; const result = TranslatorUtil.unprotectMustacheVariablesTranslation(text); expect(result).toEqual( '{{#facebook}}Olá {{leadName}} você está no Facebook{{/facebook}}{{#whatsapp}}Olá {{leadName}} você está no WhatsApp{{/whatsapp}}', ); }); }); describe('Text sanitizing', () => { it('Removing prevent translation marker properly', () => { const texts = MockGenerator.textsWithPreventTranslationsMarkers(); texts.forEach(([actual, expected]) => { actual = removePreventTranslationMarkers(actual); expect(actual).toEqual(expected); }); }); });