Add home-made testing suite

pull/3/head
KaKi87 2 years ago
parent f684f108fe
commit 00031739ca
  1. 6
      package.json
  2. 144
      test.js
  3. 5
      yarn.lock

@ -24,5 +24,11 @@
"dependencies": {
"axios": "^0.19.2",
"puppeteer": "^1.15.0"
},
"devDependencies": {
"consola": "^2.15.0"
},
"scripts": {
"test": "env CONSOLA_LEVEL=4 node test.js"
}
}

@ -0,0 +1,144 @@
(async () => {
const t1 = Date.now();
const
consola = require('consola'),
axios = require('axios');
const { getSupportedLanguages, translate, quit } = require('.');
const eventCount = {
total: 0,
success: 0,
warning: 0,
error: 0
};
const success = (...args) => {
eventCount.total++;
eventCount.success++;
consola.success(...args);
};
const warning = (...args) => {
eventCount.total++;
eventCount.warning++;
consola.warn(...args);
};
const error = (...args) => {
eventCount.total++;
eventCount.error++;
consola.error(...args);
};
consola.info('Starting test suite');
consola.info('Testing supported languages');
const { sourceLanguages, targetLanguages } = await getSupportedLanguages();
if(Array.isArray(sourceLanguages) && sourceLanguages.every(sourceLanguage => /^[a-z]{2}$/.test(sourceLanguage)))
success('Source languages are valid');
else
return error('Source languages are invalid');
if(Array.isArray(targetLanguages) && targetLanguages.every(targetLanguage => /^[a-z]{2}-[A-Z]{2}$/.test(targetLanguage)))
success('Target languages are valid');
else
return error('Target languages are invalid');
consola.info('Downloading pangrams');
let pangrams;
try {
pangrams = (await axios('https://raw.githubusercontent.com/hyvyys/language-data/master/dist/language-data.json'))
.data.reduce((res, item) => Object.assign(res, { [item['htmlTag']]: item['pangrams'][0] }), {});
} catch(_){
return error('Pangrams download failed');
}
consola.info('Testing translations');
for(let sourceLanguageIndex = 0; sourceLanguageIndex < sourceLanguages.length; sourceLanguageIndex++){
const sourceLanguage = sourceLanguages[sourceLanguageIndex];
const pangram = pangrams[sourceLanguage];
if(!pangram){
warning(`No pangram available for ${sourceLanguage}`);
continue;
}
consola.info(`Testing translations from ${sourceLanguage}`);
for(let targetLanguageIndex = 0; targetLanguageIndex < targetLanguages.length; targetLanguageIndex++){
const targetLanguage = targetLanguages[targetLanguageIndex];
const shortTargetLanguage = targetLanguage.split('-')[0];
if(sourceLanguage === shortTargetLanguage) continue;
consola.info(`Testing translation from ${sourceLanguage} to ${targetLanguage}`);
let translation;
try {
translation = await translate(pangram, sourceLanguage, targetLanguage);
}
catch(_){
error('Translation failed');
continue;
}
const { source, target } = translation;
if(source.lang !== sourceLanguage)
error('Result source language does not match request source language');
if(target.lang !== shortTargetLanguage)
error('Result target language does not match request target language');
consola.debug(`Translation : ${target.translation}`);
}
}
consola.info('Quitting');
await quit();
consola.info('Test suite completed with '
+ `${eventCount.success} successes (${(eventCount.success / eventCount.total * 100).toFixed(0)}%), `
+ `${eventCount.warning} warnings (${(eventCount.warning / eventCount.total * 100).toFixed(0)}%), and`
+ `${eventCount.error} errors (${(eventCount.error / eventCount.total * 100).toFixed(0)}%) `
+ `in ${(Date.now() - t1) / 1000} seconds.`
);
})();

@ -54,6 +54,11 @@ concat-stream@1.6.2:
readable-stream "^2.2.2"
typedarray "^0.0.6"
consola@^2.15.0:
version "2.15.0"
resolved "https://registry.yarnpkg.com/consola/-/consola-2.15.0.tgz#40fc4eefa4d2f8ef2e2806147f056ea207fcc0e9"
integrity sha512-vlcSGgdYS26mPf7qNi+dCisbhiyDnrN1zaRbw3CSuc2wGOMEGGPsp46PdRG5gqXwgtJfjxDkxRNAgRPr1B77vQ==
core-util-is@~1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"

Loading…
Cancel
Save