import 'package:eaimar/data/services/language_configs/azure_language_config.dart'; import 'package:eaimar/data/services/language_manager.dart'; import 'package:eaimar/data/services/microsoft_text_translation.dart'; import 'package:flutter_test/flutter_test.dart'; /// 界面语言表补上微软三表里原先没开放的 40 种。 /// 通话只要一边是这些语种,就整对走微软,原有语种的档位不动。 void main() { late LanguageManager languages; setUp(() { languages = LanguageManager(); }); test('语言表是原来的 39 种加上这 40 种', () { expect(languages.getAllChineseNames(), hasLength(79)); expect(LanguageManager.azureCallOnlyShortCodes, hasLength(40)); for (final code in LanguageManager.azureCallOnlyShortCodes) { expect(languages.getLanguageByShortCode(code), isNotNull, reason: code); } }); test('粤语和香港繁体共用识别码,按名字仍是两条', () { final yue = languages.getLanguageByChineseName('粤语'); final hk = languages.getLanguageByChineseName('中文(香港繁体)'); expect(yue?.shortCode, 'yue'); expect(yue?.asrCode, 'zh-HK'); expect(yue?.ttsVoiceName, 'yue-CN-XiaoMinNeural'); expect(hk?.shortCode, 'zh-Hant-hk'); expect(hk?.asrCode, 'zh-HK'); expect(hk?.ttsVoiceName, 'zh-HK-HiuMaanNeural'); // 按识别码反查保留先登记的香港繁体,系统语言默认值还指得上它。 expect(languages.getChineseNameByAsrCode('zh-HK'), '中文(香港繁体)'); expect(languages.getChineseNameByShortCode('yue'), '粤语'); }); test('原有通话档位不变,扩展语种整对走微软', () { expect(languages.findBestMatchingProvider('zh', 'en')?.provider, 'alibaba35'); expect(languages.findBestMatchingProvider('zh', 'el')?.provider, 'alibaba'); expect(languages.findBestMatchingProvider('zh', 'ro')?.provider, 'azure'); for (final code in ['fil', 'he', 'is', 'yue', 'cy', 'jv']) { expect(languages.findBestMatchingProvider('zh', code)?.provider, 'azure', reason: 'zh -> $code'); expect(languages.findBestMatchingProvider(code, 'en')?.provider, 'azure', reason: '$code -> en'); } }); test('爪哇语的微软翻译码是 jv', () { final spec = getAzureTranslationLanguageSpecs() .firstWhere((e) => e['shortCode'] == 'jv'); expect(spec['translationCode'], 'jv'); expect(languages.microsoftTranslateCode('yue'), 'yue'); expect(languages.microsoftTranslateCode('zh'), 'zh-Hans'); expect(languages.microsoftTranslateCode('zh-Hant-hk'), 'zh-Hant'); expect(languages.microsoftTranslateCode('jv'), 'jv'); }); test('同声面对面音视频的扩展语种走微软文本翻译,原有语种和通话不走', () { for (final mode in ['simultaneous', 'faceToFace', 'audioVideo']) { expect(LanguageManager.microsoftTextPipeline(mode, 'zh', 'fil'), isTrue); expect(LanguageManager.microsoftTextPipeline(mode, 'yue', 'en'), isTrue); expect(LanguageManager.microsoftTextPipeline(mode, 'zh', 'en'), isFalse); expect(LanguageManager.microsoftTextPipeline(mode, 'zh', 'el'), isFalse); } expect(LanguageManager.microsoftTextPipeline('call', 'zh', 'fil'), isFalse); }); test('粤语播报用粤语音色,不串到香港繁体', () { expect(languages.getTtsVoiceNameByShortCode('yue'), 'yue-CN-XiaoMinNeural'); expect(languages.getTtsVoiceNameByShortCode('zh-Hant-hk'), 'zh-HK-HiuMaanNeural'); }); test('繁体、乌克兰语、克罗地亚语走微软翻译码', () { expect(microsoftMtRequired('en-US', 'zh-TW'), isTrue); expect(microsoftMtRequired('zh-HK', 'en-US'), isTrue); expect(microsoftMtRequired('zh-CN', 'uk-UA'), isTrue); expect(microsoftMtRequired('hr-HR', 'en'), isTrue); expect(microsoftMtRequired('zh-CN', 'en-US'), isFalse); expect(microsoftMtCode('zh-TW'), 'zh-Hant'); expect(microsoftMtCode('zh-HK'), 'zh-Hant'); expect(microsoftMtCode('zh-CN'), 'zh-Hans'); expect(microsoftMtCode('uk-UA'), 'uk'); expect(microsoftMtCode('hr-HR'), 'hr'); expect(microsoftMtCode(''), ''); }); test('解析微软翻译响应', () { expect( microsoftTranslationText([ { 'translations': [ {'text': ' hello ', 'to': 'en'} ] } ]), 'hello', ); expect(microsoftTranslationText([]), isNull); expect(microsoftTranslationText([ {'translations': []} ]), isNull); }); }