'use strict' ; import assert from 'assert' ; import { TranslateResult , DetectResult } from '../../../app/plugin/translate/AppTranslateManager' ; import { prepareTestApplication , testClear } from '../../Util' ; import { Application } from 'egg' ; describe ( 'AppTranslateManager' , ( ) => { let app ; beforeEach ( async ( ) => { ( { app } = await prepareTestApplication ( ) ) ; } ) ; afterEach ( ( ) => { testClear ( app ) ; } ) ; it ( 'Test translate string correct' , async ( ) => { ( app . translate as any ) . newTranslator = ( _key ) => { return { translate : ( strings , _target , cb ) => { const result = { translatedText : 'apple' , originalText : strings , detectedSourceLanguage : 'zh-CN' , } ; cb ( null , result ) ; } , } ; } ; const res = await app . translate . translate ( '' , '苹果' , 'en' ) ; assert . strictEqual ( res . translatedText , 'apple' ) ; } ) ; it ( 'Test translate string array correct, array length = 1' , async ( ) => { ( app . translate as any ) . newTranslator = ( _key ) => { return { translate : ( strings , _target , cb ) => { const result = { translatedText : '苹果' , originalText : strings , detectedSourceLanguage : 'en' , } ; cb ( null , result ) ; } , } ; } ; const originalText = [ 'apple' ] ; const expectedText = [ '苹果' ] ; const target = 'zh-CN' ; const res = await app . translate . translate ( '' , originalText , target ) ; assert . strictEqual ( Array . isArray ( res ) , true ) ; for ( const i in originalText ) { assert . strictEqual ( res [ i ] . translatedText , expectedText [ i ] ) ; } } ) ; it ( 'Test translate string array correct, array length > 1' , async ( ) => { ( app . translate as any ) . newTranslator = ( _key ) => { return { translate : ( strings , _target , cb ) => { const result = [ { translatedText : '苹果' , originalText : strings [ 0 ] , detectedSourceLanguage : 'en' , } , { translatedText : '香蕉' , originalText : strings [ 1 ] , detectedSourceLanguage : 'en' , } , ] ; cb ( null , result ) ; } , } ; } ; const originalText = [ 'apple' , 'banana' ] ; const expectedText = [ '苹果' , '香蕉' ] ; const target = 'zh-CN' ; const res = await app . translate . translate ( '' , originalText , target ) ; assert . strictEqual ( Array . isArray ( res ) , true ) ; for ( const i in originalText ) { assert . strictEqual ( res [ i ] . translatedText , expectedText [ i ] ) ; } } ) ; describe ( 'Test error hanppend when translate' , ( ) => { beforeEach ( async ( ) => { ( app . translate as any ) . newTranslator = ( _key ) => { return { translate : ( _strings , _target , cb ) => { cb ( 'ERROR_TEST_TRANSLATE' ) ; } , } ; } ; } ) ; it ( 'translate string error' , async ( ) => { const res = await app . translate . translate ( '' , '苹果' , 'en' ) ; assert . strictEqual ( res , undefined ) ; } ) ; it ( 'test translate array error' , async ( ) => { const res = await app . translate . translate ( '' , [ 'apple' , 'banana' ] , 'zh-CN' ) ; assert . strictEqual ( res , undefined ) ; } ) ; } ) ; it ( 'Test detectLanguage correct' , async ( ) => { ( app . translate as any ) . newTranslator = ( _key ) => { return { detectLanguage : ( strings , cb ) => { const result = { language : 'en' , isReliable : true , confidence : 1 , originalText : strings , } ; cb ( null , result ) ; } , } ; } ; const res = await app . translate . detectLanguage ( '' , 'apple' ) ; assert . strictEqual ( res . language , 'en' ) ; } ) ; it ( 'Test error hanppend when detectLanguage' , async ( ) => { ( app . translate as any ) . newTranslator = ( _key ) => { return { detectLanguage : ( _strings , cb ) => { cb ( 'ERROR_TEST_DETECTLANGUAGE' ) ; } , } ; } ; const res = await app . translate . detectLanguage ( '' , 'apple' ) ; assert . strictEqual ( res , undefined ) ; } ) ; } ) ;